|
-
Nov 2nd, 2005, 01:56 PM
#1
Thread Starter
Addicted Member
vb.net compound if statement. how?
Grrrrrrrrrrr... I keep getting errors.
I can't figure out how to use the & / or operators in an if statement.
Code:
if (x < Y) & (y > Z) then
... yadda
end if
???
-
Nov 2nd, 2005, 01:57 PM
#2
Hyperactive Member
Re: vb.net compound if statement. how?
-
Nov 2nd, 2005, 02:01 PM
#3
Re: vb.net compound if statement. how?
VB Code:
If X < Y And Y > Z Then
'do code if both is true
End If
If X < Y Or Y > Z Then
'do code if one is true
End If
-
Nov 2nd, 2005, 02:16 PM
#4
Thread Starter
Addicted Member
Re: vb.net compound if statement. how?
Thanks guys.
-
Nov 2nd, 2005, 03:38 PM
#5
Re: vb.net compound if statement. how?
You can also use the commonly forgotten( AndAlso OrElse) operators.
-
Nov 2nd, 2005, 07:33 PM
#6
Re: vb.net compound if statement. how?
You absolutely SHOULD use AndAlso and OrElse. Those from a VB6 background are used to And and Or but things have changed. You should only use And and Or if you specifically want the second expression to be evaluated even if the first fails. In most cases it won't matter, and the miniscule improvement in performance won't be noticed, but there are instances where it is the difference between an unhandled exception or not, e.g.
VB Code:
If myForm Is Nothing OrElse myForm.IsDisposed Then
This code checks whether a form exists by testing whether it has never been created or it has been destroyed. If the first condition is True then the second condition is not evaluated, which is desireable. If Or was used instead, the second condition would still be evaluated and a NullReferenceException thrown. The moral of the story is that you should ALWAYS use AndAlso and OrElse unless there is a specific need to use And and Or.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|