Simple SELECT CASE Question

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

Guest

Does anyone know why this doesn't work:


Select Case X
Case X or Y
msgbox("hello world!")
End Select


It seems X or Y doesn't evaluate correctly (I think only the first 1/2 is
evaluating).

Instead, I believe the proper syntax is Case X, Y?

Can anyone clarify why X or Y will compile but doesn't evaluate right?
 
Spam said:
Does anyone know why this doesn't work:


Select Case X
Case X or Y
msgbox("hello world!")
End Select


It seems X or Y doesn't evaluate correctly (I think only the first 1/2 is
evaluating).

Instead, I believe the proper syntax is Case X, Y?

Can anyone clarify why X or Y will compile but doesn't evaluate right?

X or Y evaluates to a boolean True or False so unless X is True or both
X and Y are False the msgbox will not display

If you want the equivalent of "If X = X or X = Y then..." you need Case
X, Y
 
Are X and Y booleans? What datatypes are they?

Additionally, if you don't have option strict on, A LOT of things will
compile in VB.NET that will then either crash at runtime or have unexpected
behavior. You need to always keep option strict on, and be careful of type
safety.
 
Does anyone know why this doesn't work:
Select Case X
Case X or Y
msgbox("hello world!")
End Select
It seems X or Y doesn't evaluate correctly (I think only the first 1/2
is evaluating).

Instead, I believe the proper syntax is Case X, Y?

Can anyone clarify why X or Y will compile but doesn't evaluate right?

You could do the following:
Select Case True
Case X or Y
MessageBox.Show("Hello World!")
End Select

I suspect however, you want the Case X, Y syntax which is the same as If
X=X or Y=X then. Actually since X=X your sample should always evaluate to
true regardless of the value of Y and show "hello world"). Instead, I would
recommend the following:

Select Case X
Case Y, Z
'Do Something
Case Else
'Do something else, or log an exception case?
End Select

Jim Wooley
http://devauthority.com/blogs/jwoole
 
Spam Catcher said:
Does anyone know why this doesn't work:

Select Case X
Case X or Y
msgbox("hello world!")
End Select

It seems X or Y doesn't evaluate correctly (I think only the first 1/2 is
evaluating).

Instead, I believe the proper syntax is Case X, Y?

Yes, it is. In the sample above the binary OR of 'X' and 'Y' is computed,
and 'X' is not equal to 'X Or Y' if Y <> 0, for example.
 
I suspect however, you want the Case X, Y syntax which is the same as
If X=X or Y=X then. Actually since X=X your sample should always
evaluate to true regardless of the value of Y and show "hello world").
Instead, I would recommend the following:

Select Case X
Case Y, Z
'Do Something
Case Else
'Do something else, or log an exception case?
End Select

Thank you - that is what I wanted : )
 
Are X and Y booleans? What datatypes are they?

Additionally, if you don't have option strict on, A LOT of things will
compile in VB.NET that will then either crash at runtime or have
unexpected behavior. You need to always keep option strict on, and be
careful of type safety.

X and Y are integers - I suspect it is time for me to turn on option
strict... : )
 
Back
Top