Home All Groups Group Topic Archive Search About

Simple SELECT CASE Question

Author
27 Jul 2006 5:17 PM
Spam Catcher
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?

Author
27 Jul 2006 5:24 PM
andrew.google
Spam Catcher wrote:
Show quoteHide quote
> 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
Author
27 Jul 2006 5:28 PM
Marina Levit [MVP]
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.

Show quoteHide quote
"Spam Catcher" <spamhoneypot@rogers.com> wrote in message
news:Xns980D871FCF2BBusenethoneypotrogers@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?
Author
28 Jul 2006 4:19 PM
Spam Catcher
"Marina Levit [MVP]" <someone@nospam.com> wrote in
news:#nXAUIasGHA.1888@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... : )
Author
27 Jul 2006 6:27 PM
Jim Wooley
> 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
Author
28 Jul 2006 4:19 PM
Spam Catcher
Jim Wooley <jimNOSPAMwooley@hotmail.com> wrote in
Show quoteHide quote
news:24f81e8f7c688c87f9ff6d0a16a@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 : )
Author
27 Jul 2006 10:00 PM
Herfried K. Wagner [MVP]
"Spam Catcher" <spamhoneypot@rogers.com> schrieb:
> 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/>