469,626 Members | 825 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Simple SELECT CASE Question

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?
Jul 27 '06 #1
6 1839
Spam Catcher wrote:
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

Jul 27 '06 #2
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.

"Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
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?

Jul 27 '06 #3
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/jwooley
Jul 27 '06 #4
"Spam Catcher" <sp**********@rogers.comschrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jul 27 '06 #5
Jim Wooley <ji*************@hotmail.comwrote in
news:24*************************@msnews.microsoft. com:
>
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 : )
Jul 28 '06 #6
"Marina Levit [MVP]" <so*****@nospam.comwrote in
news:#n**************@TK2MSFTNGP03.phx.gbl:
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... : )
Jul 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Scott | last post: by
5 posts views Thread by Stephanie_Stowe | last post: by
5 posts views Thread by Tim::.. | last post: by
7 posts views Thread by Scott Frankel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.