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
*** Sent via Developersdex http://www.developersdex.com ***
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?
C# doesn't support this VB idiom, you can use an if statement though:
if (X)
{
s1 = "a";
}
else if (Y)
{
s1 = "n";
}
--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
The trouble with computers, of course, is that they're very
sophisticated idiots.
-- The Doctor in Doctor Who:"The Robot"
> 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! |
*******************************************
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
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
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"
"Tom Dacon" <tda...@community.nospam> wrote in message
news:eifXb55F...@TK2MSFTNGP03.phx.gbl...
>
> "Rich P" <rpn...@aol.com> wrote in message
> news:O0KCVu4F...@TK2MSFTNGP03.phx.gbl...
>> 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
>>
>> *** Sent via Developersdex http://www.developersdex.com ***
>
> 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.
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
> 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
--
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
> 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.
Well, I'll be darned.
Tom
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