mdiglio
asked on
behavior of select case .net 2005
Hello,
Can someone explain to me why this select case statement does not have to evaluate all the conditions when I insert a breakpoint and step through it?
It will just jump right to the exact one it evaluates as true even if its the last one.
If I change the select statement to be something like this it will stop at each case expressionlist
case 10
'do something
case 25
'do something
Or if I change it to strings it will stop at each case expressionlist
A link would be great!
Dim x As Integer
x = 25
Select Case x
Case 1 To 10
Debug.Print(x)
Case 11 To 20
Debug.Print(x)
Case 21 To 30
Debug.Print(x)
Case Else
Debug.Print(x)
End Select
Thank you!
Can someone explain to me why this select case statement does not have to evaluate all the conditions when I insert a breakpoint and step through it?
It will just jump right to the exact one it evaluates as true even if its the last one.
If I change the select statement to be something like this it will stop at each case expressionlist
case 10
'do something
case 25
'do something
Or if I change it to strings it will stop at each case expressionlist
A link would be great!
Dim x As Integer
x = 25
Select Case x
Case 1 To 10
Debug.Print(x)
Case 11 To 20
Debug.Print(x)
Case 21 To 30
Debug.Print(x)
Case Else
Debug.Print(x)
End Select
Thank you!
ASKER
Thanks for the reply.
I don't think this answers what I am after.
If it does answer it then I would need it explained
Why when I step through the above code ( pressing F8 ) do these expressionlists never get evaluated...case 1 to 10 | case 11 to 20
it just jumps to the one that evaluates as true...Case 21 To 30. How does it know?
If I remove the TO keyword it will stop at each line until it evaluates to true.
I don't think this answers what I am after.
If it does answer it then I would need it explained
Why when I step through the above code ( pressing F8 ) do these expressionlists never get evaluated...case 1 to 10 | case 11 to 20
it just jumps to the one that evaluates as true...Case 21 To 30. How does it know?
If I remove the TO keyword it will stop at each line until it evaluates to true.
My guess - and I stress it is no more than that - is that it is to do with the difference between value types (e.g. integers) and reference types (e.g. strings). In part, that guess is because - unlike you report - whenever I have just tested with integers (whether single - e.g. Case 25 - or banded - e.g. Case 21 To 30) program flow jumps straight to the "right" answer, if there is one. My thinking is that, with a value type, in effect the "answer is there in the question": but with reference types, the program has to look up the value that is to be compared, all it has is a reference to that value.
Roger
Roger
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks to everyone for your responses
ASKER
AlexFM ,
Is there an easy way for you to explain how you got the above code.
Or should that be asked as another question?
Is there an easy way for you to explain how you got the above code.
Or should that be asked as another question?
Run Microsoft IldAsm program:
Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\ildasm.exe
and open .NET Assembly (.exe or .Dll). Read more about ILDASM and MSIL if you are interesting in .NET internals.
Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\ildasm.exe
and open .NET Assembly (.exe or .Dll). Read more about ILDASM and MSIL if you are interesting in .NET internals.
http://msdn2.microsoft.com/en-us/library/cy37t14y.aspx
Remarks
If testexpression matches any Case expressionlist clause, the statements following that Case statement run up to the next Case, Case Else, or End Select statement. Control then passes to the statement following End Select. If testexpression matches an expressionlist clause in more than one Case clause, only the statements following the first match run.
...
Note
A Case statement with multiple clauses can exhibit behavior known as short-circuiting. Visual Basic evaluates the clauses from left to right, and if one produces a match with testexpression, the remaining clauses are not evaluated. Short-circuiting can improve performance, but it can produce unexpected results if you are expecting every expression in expressionlist to be evaluated. For more information on short-circuiting, see Boolean Expressions.