[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

boolean expressions evaluation

Thomas Lajeunesse

10/15/2002 2:22:00 PM

Hello,

In the book "ASP.NET Professional" from Wrox, it is said (Chapter 3, in
the part about Visual Basic .NET) that multiple boolean expressions in a
IF statement have their execution shortened. For instance, in an AND
expression, if the first part of the expression is False, the remaining
elements are not evaluated, since the result would already be known as
False. The same occurs with an OR statement, if the first part is True.

This is what is explained in the book... But when I test it, it works as
in Visual Basic 6, both parts of the expression always being evaluated,
whatever the first part is, True or False.

Does someone knows something about this ?

Here is the code sample I used for this test :
Public Structure Element
Dim Value As Boolean
Dim NbAccess As Integer
End Structure
Dim TabElts(2) As Element
Public Function IsTrue(ByVal i As Integer) As Boolean
TabElts(i).NbAccess = TabElts(i).NbAccess + 1
IsTrue = TabElts(i).Value
End Function
Public Sub Main()
TabElts(0).Value = True
TabElts(0).NbAccess = 0
TabElts(1).Value = True
TabElts(1).NbAccess = 0
If IsTrue(0) Or IsTrue(1) Then
MsgBox("At least one element is true.")
End If
MsgBox("After <If IsTrue(0) Or IsTrue(1) Then> : 0:" &
TabElts(0).NbAccess.ToString & " 1:" & TabElts(1).NbAccess.ToString)
End Sub

Regards,

*** Sent via Developersdex http://www.develop... ***
Don't just participate in USENET...get rewarded for it!
1 Answer

Mattias Sjögren

10/15/2002 5:11:00 PM

0

Thomas,

>Does someone knows something about this ?

The behaviour described in the book was true for beta versions of
VB.NET, but in the release version the And and Or operators does not
short circuit. Instead, there are two new operators for this: AndAlso
and OrElse.



Mattias

===
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.n...
Please reply only to the newsgroup.