A
Al G
Ok, so I've started to use it, and it is quicker.
Now, why is it that "AND" doesn't already work this way?
Al G
Now, why is it that "AND" doesn't already work this way?
Al G
Ok, so I've started to use it, and it is quicker.
Now, why is it that "AND" doesn't already work this way?
speed up your code, although that can be a pleasant side effect. IFrom what I can tell, the primary reason to use andalso is not to
Michel Posseth said:read this and you know it all
http://www.panopticoncentral.net/articles/919.aspx ( and maybe more
hth
Michel
Ok, but if Foo() evaluates to a boolean value, as suggested by theStephany Young said:But ...
If Foo() returns an integer than it is evaluatable as a boolean.
A value of 0 will evaluate to False - A value of anything elese will
evaluate to True (or, more correctly, Not False).
If Foo() And Bar()
will check both expressions, whereas:
If Foo() AndAlso Bar()
will only check Bar() if Foo() evaluates to True.
Al G said:Ok, but if Foo() evaluates to a boolean value, as suggested by the
syntax,
why check Bar(). In other words, why would you want to do it the old
way?
Michel Posseth said:For logical operators i always use OrElse , AndAlso
if i need a bitwise comparisation i use Or , And
Al G <agerhart2 said:Ok, so I've started to use it, and it is quicker.
Now, why is it that "AND" doesn't already work this way?
Michael D. Ober said:In the cases of AndAlso vs. And and OrElse vs. Or, the issue MS had to
struggle with was that VB 6 and earlier didn't short circuit the conditional
evaluation and a lot of existing code depended on this behavior.
Even C and
C++ have non-short circuit versions of And ( & ) and Or ( | ) - they're
simply not used much.
Changing the behavior of And and Or would have caused
nearly 100% of all VB 6 programs to break upon porting to VB.Net (any
version).
Who did write that it was not like that?"Logical" doesn't mean "short circuiting". 'Or' and 'And' are logical and
bitwise, depending on the situation.
Michael D. Ober said:In the cases of AndAlso vs. And and OrElse vs. Or, the issue MS had to
struggle with was that VB 6 and earlier didn't short circuit the
conditional evaluation and a lot of existing code depended on this
behavior. Even C and C++ have non-short circuit versions of And ( & ) and
Or ( | ) - they're simply not used much. Changing the behavior of And and
Or would have caused nearly 100% of all VB 6 programs to break upon
porting to VB.Net (any version).
Mike Ober
Michael D. Ober said:In the cases of AndAlso vs. And and OrElse vs. Or,
snip
Changing the behavior of And and Or would have caused nearly 100% of all
VB 6 programs to break upon porting to VB.Net (any version).
Is this true? I assume you mean bitwise use of "and".
I haven't been able to find a single example of code that would require
"and" as opposed to andalso.
To put it another way:
Are there any circumstances where I would want to use the old
"and"?
Al G
EventArgs) Handles placeOrder.Click
If IsItemOnHand() And ItemCanBeShipped() Then
End If
End Sub
rowe_newsgroups said:Very rarely would I ever use the "old and". Very few of my projects
don't make use of the short-circuit evaluation of the AndAlso
statement. But consider the following:
////////////////////
Public Sub placeOrder_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles placeOrder.Click
If IsItemOnHand() And ItemCanBeShipped() Then
End If
End Sub
Public Function IsItemOnHand() As Boolean
'// Check a database in a real world application
'// Here I'm simulating a failure
Dim onHand As Boolean = False
'// In a real world app, you could log an event if
'// the item wasn't on hand when the order was placed
If Not onHand Then LogEvent("Order Attempted on Unavailable
Item")
Return onHand
End Function
Public Function ItemCanBeShipped() As Boolean
'// Check a database in a real world application
'// Here I'm simulating a failure
Dim canBeShipped As Boolean = False
'// In a real world app, you could log an event if
'// the item wasn't on hand when the order was placed
If Not canBeShipped Then LogEvent("Order Attempted on
Unshippable Item")
Return canBeShipped
End Function
Public Sub PlaceOrder()
'// Save the order to a database in a real world app
End Sub
Public Sub LogEvent(ByVal eventDescription As String)
'// Log the event here
End Sub
/////////////////////
As you'll see the code simulates an order placement class. Two checks
are made when the user clicks the button to place an order, first it
checks to see if the item is available, and second it checks to see it
the item can be shipped. In each function, an event is logged if the
item doesn't meet the necessary requirements. In a real world
application these events would be monitored and used to prevent
unavailable and unshippable items from being ordered.
Using the "And" statement forces both functions to fire - which in
this case is good as it forces a check to make sure the item is also
shippable, even if the item wasn't on hand. If we switch to "AndAlso"
only the the unavailable item event would be logged, thus hiding an
important message (unshippable item) from the monitoring - which could
cause more problems when the unshippable item becomes available.
I realize that might be a confusing example - so please ask if you
need me to clarify any points.
Thanks,
Seth Rowe