If statement is false but code doesn't execute

row118

Member
Joined
Feb 4, 2006
Messages
21
Programming Experience
5-10
:eek:
I've not been to sleep in 18 hours.
Pseudocode for the problem portion of the statement:
if field is required there is no data
return an error message

What's is this statement returning true.? When I take it thru debugger, the first test is false yet


Code:
VB.NET:
dim val as string = ""
dim _required as boolean = false 
dim error as string = ""

If (_required) And (Trim(val).Length = 0) Then
     error = _reqMsg
     Return False
ElseIf (val.Length < _minLength) Then
      ErrorMessage = _minLengthMsg
      Return False
else 
    return true
end if

???
 
When I looked back at this with a fresh eye this a.m. what I wrote makes no sense. The "If" statement is evaluating to true although the _required = false. What I am trying to accomplish is: give an error message only when the field is required and length is 0. My eye is not fresh enough to catch the error in my logic. by the way, the variable ErrorMessage is "error" in the actual code.
 
how about:

VB.NET:
If _required = True
  If val.Trim.Length = 0 Then
    error = _reqMsg
    Return False
  End If
  If val.Trim.Length < _minLength
    error = _minLengthMsg
    Return False
  End If
Else
  Return True
End If
 
Back
Top