translate VB select case to switch case problem

R

Rich P

VB.Net supports the following select statement(s)

Dim X, Y As Boolean
Dim s1 As String

Select Case True
Case X
s1 = "a"
Case Y
s1 = "b
End Select

In this select the variables X and Y can be used in the case statement.
When I tried to translate this to C# I got an error message that a
constant was expected in the case statement. X can be true when Y is
false and Y can be true when X is false. Is there an equivalent
construct in C# or how would I want to handle something like this in C#?

Thanks,

Rich
 
R

Rich P

I ended up going with

if (X.Equals(true)) s1 = "a";
if (Y.Equals(true)) s1 = "b";

But I still ask if there is an equivalent C# construct to the VB.Net

Select Case True
Case X
s1 = "a"
Case Y
s1 = "b
End Select

or does the switch construct strictly use constants in the case
statement?

Rich
 
S

Scott Seligman

Rich P said:
Select Case True
Case X
s1 = "a"
Case Y
s1 = "b
End Select

C# doesn't support this VB idiom, you can use an if statement though:

if (X)
{
s1 = "a";
}
else if (Y)
{
s1 = "n";
}
 
G

Gregory A. Beamer

VB.Net supports the following select statement(s)

Dim X, Y As Boolean
Dim s1 As String

Select Case True
Case X
s1 = "a"
Case Y
s1 = "b
End Select

In this select the variables X and Y can be used in the case statement.
When I tried to translate this to C# I got an error message that a
constant was expected in the case statement. X can be true when Y is
false and Y can be true when X is false. Is there an equivalent
construct in C# or how would I want to handle something like this in C#?


So, I was thinking about becomming a VB programmer. Thanks for showing me
the folly of my ways. ;-)

With a quick test, I see VB, as expected, short circuits the select when X
is true, revealing the answers

True, True A
True, False A
False, True B

So the correct code to match these answers is:

if(X)
s1="a";
else
{
if(Y)
s1="b";
}

Which can also be shortened to:

if(X)
s1="a";
else if (Y)
s1="b";



--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
A

Alexander Mueller

Rich said:
VB.Net supports the following select statement(s)

Dim X, Y As Boolean
Dim s1 As String

Select Case True
Case X
s1 = "a"
Case Y
s1 = "b
End Select

In this select the variables X and Y can be used in the case statement.
When I tried to translate this to C# I got an error message that a
constant was expected in the case statement. X can be true when Y is
false and Y can be true when X is false. Is there an equivalent
construct in C# or how would I want to handle something like this in C#?


Actually this is a mis-use of the select case/switch-statement.
Usually it discriminates between different expected states
of the same "thing", not between arbitrary states of anything,
although it's of course syntactically legal.


Shorthand-equivalent in C# is:

string s1 = X ? "a" : Y ? "b" : null;

Even in VB the proper expression would be a VB-Switch Statement
(kind f an extended IIf, most VB-programmers don't even know it ;-) ):

//VB
String s1 = Switch(X, "a", Y, "b")

MfG,
Alex
 
T

Tom Dacon

Rich P said:
VB.Net supports the following select statement(s)

Dim X, Y As Boolean
Dim s1 As String

Select Case True
Case X
s1 = "a"
Case Y
s1 = "b
End Select

In this select the variables X and Y can be used in the case statement.
When I tried to translate this to C# I got an error message that a
constant was expected in the case statement. X can be true when Y is
false and Y can be true when X is false. Is there an equivalent
construct in C# or how would I want to handle something like this in C#?

Thanks,

Rich

What none of the other respondents has pointed out is that the C# switch
statement is limited to numeric values, including members of enumerations,
in the switch argument (not just numeric constants or enumeration members).
VB's Select Case statement offers the useful ability to use string variables
or string constants in its Case statements, but C# does not support an
equivalent other than a series of if / else if / else if statements. Too
bad, because it's not uncommon to want to do something like that. The net
effect on performance of choosing case statements vs. a series of if /else
if statements is insignificant, usually. However from a coding style
viewpoint many prefer the structure of a switch/Select Case in situations
where there's more than two or three cases that you want to test. I wouldn't
surprise me if C# eventually added the capability - the language developers
are competitive and each has been known to adopt features that the other
language offers.

Tom Dacon
Dacon Software Consulting
 
S

Scott Seligman

Tom Dacon said:
What none of the other respondents has pointed out is that the C# switch
statement is limited to numeric values, including members of enumerations,
in the switch argument (not just numeric constants or enumeration members).

No one pointed it out because that's not correct.

C# supports switch statements using sbyte, byte, short, ushort, int,
uint, long, ulong, char, or string as the value type.
 
A

Aaron

Tom Dacon said:
What none of the other respondents has pointed out is that the C# switch
statement is limited to numeric values, including members of enumerations,
in the switch argument (not just numeric constants or enumeration
members).

I beg to differ with you Tom. I don't know if it is a difference between 1.1
framework and 2.0 + versions but I have often used the C# switch statement
with string values:

public void ButtonClick(object sender, EventArgs e)
{
string StringButtonClicked = "";
switch (((Button)sender).Name.ToString().ToUpper())
{
case "BUTTON1":
StringButtonClicked = "Number 1 Button";
break;

case "BUTTON2":
StringButtonClicked = "Number 2 Button";
break;


case "BUTTON3":
StringButtonClicked = "Number 3 Button";
break;

}//end switch
MessageBox.Show("ButtonClicked: " + StringButtonClicked);
}//end ButtonClick

Specifically I like to use it in Button click events.
 
A

Arne Vajhøj

Aaron said:
I beg to differ with you Tom. I don't know if it is a difference between
1.1 framework and 2.0 + versions but I have often used the C# switch
statement with string values:

1.1 supported switch on string.

I would be very surprised if 1.0 did not as well, but I have
never used 1.0.

Arne
 
P

Peter Duniho

What none of the other respondents has pointed out is that the C# switch
statement is limited to numeric values, including members of
enumerations,
in the switch argument (not just numeric constants or enumeration
members).
VB's Select Case statement offers the useful ability to use string
variables
or string constants in its Case statements, but C# does not support an
equivalent other than a series of if / else if / else if statements. Too
bad, because it's not uncommon to want to do something like that.

It's not even uncommon in C#, where System.String _is_ in fact a supported
type for a switch/case statement. No one else pointed out the lack of
support for that in C#, because it's not entirely true.

It's true that a case statement cannot have a variable; it has to be a
compile-time constant. And that in fact is what's relevant in this
question, I believe. But strings are definitely a supported type.
The net
effect on performance of choosing case statements vs. a series of if
/else
if statements is insignificant, usually.

Hardly. The C# compiler will optimize a switch/case into jump tables or a
dictionary, providing O(1) performance characteristics instead of the O(n)
that if/else would.

Any insignificance would be a result of a very small "n", rather than a
difference in implementation detail. But obviously even for relatively
small "n" (say dozens) there must be a performance improvement, given that
the compiler team saw fit to include such an optimization.

Ironically, there's no way for VB.NET to include an optimization like this
if variables are used in case statements. So even if it has this sort of
optimization normally, it would be disabled in the usage shown in this
topic.
However from a coding style
viewpoint many prefer the structure of a switch/Select Case in situations
where there's more than two or three cases that you want to test. I
wouldn't
surprise me if C# eventually added the capability - the language
developers
are competitive and each has been known to adopt features that the other
language offers.

C# has allowed strings in switch/case since v1.0.

I doubt C# will ever support variables in case statements; it's completely
contrary to the concept of a relatively simple language without hidden
performance traps.

Pete
 
D

David Anton

You're thinking of Java and C++ - the C# switch is supported for string types
in addition to integer types.

--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
 
J

Jeff Johnson

VB.Net supports the following select statement(s)
Select Case True

For what it's worth, there are VB programmers who happen to think that the
"creative use" of VB's Select Case statement as demonstrated above is an
abomination. The discussion has come up several times in the Classic VB
group.
 
T

Tom Dacon

Scott Seligman said:
No one pointed it out because that's not correct.

C# supports switch statements using sbyte, byte, short, ushort, int,
uint, long, ulong, char, or string as the value type.

--
--------- Scott Seligman <scott at <firstname> and michelle dot
net> ---------
Circular logic will only make you dizzy, Doctor.
-- Peri in Doctor Who:"The Two Doctors"

Well, I'll be darned.

Tom
 
T

Tom Dacon

David Anton said:
You're thinking of Java and C++ - the C# switch is supported for string
types
in addition to integer types.

Sorry about that, folks. I actually tried to load the C# ECMA spec from the
help file to check on it before I posted but got a broken link, and then
went from memory.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top