Solved

behavior of select case .net 2005

Posted on 2006-11-22
7
254 Views
Last Modified: 2012-06-22
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!
0
Comment
Question by:mdiglio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 13

Expert Comment

by:newyuppie
ID: 17995806
from:
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.
 
0
 
LVL 16

Author Comment

by:mdiglio
ID: 17996047
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.


       
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17996067
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
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17996112
This is MSIL code produced by compiler for select case fragment:

.method public static void  Main() cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
  // Code size       198 (0xc6)
  .maxstack  2
  .locals init ([0] int32 x,
           [1] int32 VB$t_i4$L0,
           [2] int32 VB$CG$t_i4$S0)
  IL_0000:  nop
  IL_0001:  ldc.i4.s   25
  IL_0003:  stloc.0
  IL_0004:  nop
  IL_0005:  ldloc.0
  IL_0006:  ldc.i4.1
  IL_0007:  sub
  IL_0008:  stloc.2
  IL_0009:  ldloc.2
  IL_000a:  switch     (
                        IL_0089,
                        IL_0089,
                        IL_0089,
                        IL_0089,
                        IL_0089,
                        IL_0089,
                        IL_0089,
                        IL_0089,
                        IL_0089,
                        IL_0089,
                        IL_0098,
                        IL_0098,
                        IL_0098,
                        IL_0098,
                        IL_0098,
                        IL_0098,
                        IL_0098,
                        IL_0098,
                        IL_0098,
                        IL_0098,
                        IL_00a7,
                        IL_00a7,
                        IL_00a7,
                        IL_00a7,
                        IL_00a7,
                        IL_00a7,
                        IL_00a7,
                        IL_00a7,
                        IL_00a7,
                        IL_00a7)
  IL_0087:  br.s       IL_00b6
  IL_0089:  nop
  IL_008a:  ldloc.0
  IL_008b:  call       string [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToString(int32)
  IL_0090:  call       void [System]System.Diagnostics.Debug::Print(string)
  IL_0095:  nop
  IL_0096:  br.s       IL_00c3
  IL_0098:  nop
  IL_0099:  ldloc.0
  IL_009a:  call       string [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToString(int32)
  IL_009f:  call       void [System]System.Diagnostics.Debug::Print(string)
  IL_00a4:  nop
  IL_00a5:  br.s       IL_00c3
  IL_00a7:  nop
  IL_00a8:  ldloc.0
  IL_00a9:  call       string [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToString(int32)
  IL_00ae:  call       void [System]System.Diagnostics.Debug::Print(string)
  IL_00b3:  nop
  IL_00b4:  br.s       IL_00c3
  IL_00b6:  nop
  IL_00b7:  ldloc.0
  IL_00b8:  call       string [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToString(int32)
  IL_00bd:  call       void [System]System.Diagnostics.Debug::Print(string)
  IL_00c2:  nop
  IL_00c3:  nop
  IL_00c4:  nop
  IL_00c5:  ret
} // end of method Module1::Main

VB Select Case is converted to MSIL switch statement. switch contains offsets for every number from 1 to 30. You can read more about MSIL switch statement here:
http://msdn2.microsoft.com/en-US/library/system.reflection.emit.opcodes.switch.aspx

Actually, it is executed by comparing x variable with numbers 1, 2, 3 ... 30. Since there is no exact match between produced MSIL code and VB code, debugger jumps directly to case line which meets the condition.
This is usual Assembly situation: there is exact way to convert VB code to MSIL, but it is impossible sometimes to reproduce VB code from MSIL.
0
 
LVL 16

Author Comment

by:mdiglio
ID: 17996211
Thanks to everyone for your responses
0
 
LVL 16

Author Comment

by:mdiglio
ID: 17996263
AlexFM ,
Is there an easy way for you to explain how you got the above code.
Or should that be asked as another question?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17996357
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question