c# equivalent to select case true

  • Thread starter Thread starter TS
  • Start date Start date
Show us a code sample in VB what you want to do.

As a guess, you're not talking about the switch keyword, are you?
 
In VB, you are able to do this since the case statements can include a wide
range of expressions. In C#, the case expressions must be constant, so the
usefulness of this technique is greatly reduced.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
 
thanks, yes i am looking for a
switch(true)
{
case var1 = 5 : ...
case var9 = 21: ...

i guess you can't do it as i thought
 
No, you can't do this in C#. Other languages have this kind of switch
construct:

switch (value)
{
case expression:
case expression:
...
default:
}

C# (as well as C, C++, and Java) has this kind of switch construct:

switch (expression)
{
case constant:
case constant:
case constant:
...
default:
}

So, no. You have to use if... else if... else if... else.
 
TS said:
thanks, yes i am looking for a
switch(true)
{
case var1 = 5 : ...
case var9 = 21: ...

i guess you can't do it as i thought
<snip>
you can:
the c# equivalent is:

if (var1 == 5)
{
.....
}
else if (var9 == 21)
{
......
}
........
 
Back
Top