Select Case and OrElse

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,

Does 'Select Case' construct perform logical short-circuiting like the
'OrElse' opeartor or does it function like the 'Or' operator?

For Example:
Dim sTeststr as String
sTeststr = "ABC"
Select Case sTeststr
Case "ABC","DEF","GHI"
....
....
End Select

Is sTeststr compared to all the values in the case, irrespective of whether
or not the value is found before scanning through all the case values?

kd
 
KD

The OrElse works like the OR in any other language than VB

It stops after that the first evaluation is true

I hope this helps?

Cor
 
kd said:
Hi All,

Does 'Select Case' construct perform logical short-circuiting like the
'OrElse' opeartor or does it function like the 'Or' operator?

For Example:
Dim sTeststr as String
sTeststr = "ABC"
Select Case sTeststr
Case "ABC","DEF","GHI"
...
...
End Select

Is sTeststr compared to all the values in the case, irrespective of
whether or not the value is found before scanning through all the
case values?

Why not find out yourself?

Dim sTeststr as String
sTeststr = "ABC"
Select Case sTeststr
Case "ABC"
MsgBox("compared to ABC")
Case "DEF"
MsgBox("compared to DEF")
End Select
 
Thanks Cor for your reply.

I think, I have not framed the question right. Let me give it a try again.

When a Case block, has several values, is the Case variable compared to all
the values, even if a match is found (before completing the entire list of
Case values)? In other words, does it function like the Or Operator in
vb.net, or is the scanning aborted if a match is found, (before scanning the
entire list) like the OrElse Opeartor?

kd
 
Cor,

I hope I got you right.

So, you mean to say that in the following example,
For Example:
Dim sTeststr as String
sTeststr = "ABC"
Select Case sTeststr
Case "ABC","DEF","GHI"
....
....
End Select
since sTeststr is matched for the first case value, sTeststr is not compared
with "DEF" or "GHI"?

kd.
 
kd said:
Does 'Select Case' construct perform logical short-circuiting like the
'OrElse' opeartor or does it function like the 'Or' operator?

Yes, the function of 'Select Case' is similar to 'OrElse'. The
expression is compared to the list of values, until the first match
occurs.
For Example:
Dim sTeststr as String
sTeststr = "ABC"
Select Case sTeststr
Case "ABC","DEF","GHI"
...
...
End Select

In your example 'Select Case' stops comparing at "ABC". The other
values are ignored.

Thorsten Doerfler
 
KD,

You are speeding up nanoseconds.

When you really wants to know you can see this what happens in the
disassembly window from the debugger, (Debug -> Windows -> Disassembly) that
shows you complete the process.

I hope this helps?

Cor
 
Back
Top