Switch Statement Help

  • Thread starter Thread starter Scott C. Reynolds
  • Start date Start date
S

Scott C. Reynolds

In VB6 you could do a SELECT CASE that would evaluate each case for
truth and execute those statements, such as:

SELECT CASE True
case x > y:
dosomestuff()
case x = 5:
dosomestuff()
case y > x:
dosomestuff()
END SELECT

such that if x were 5 and y were 3, it would execute both statements
that evaluated to true.

I have found no way to do the same or similar using Switch. Is there any?
 
<ciach>

RTFM!!!!!!!

from
MSDN(ms-help://MS.VSCC/MS.MSDNVS/csref/html/vclrfTheSwitchStatement.htm)

switch (expression)
{
case constant-expression:
statement
jump-statement
[default:
statement
jump-statement]
}


pozdrawiam
 
Selvin said:
<ciach>

RTFM!!!!!!!

from
MSDN(ms-help://MS.VSCC/MS.MSDNVS/csref/html/vclrfTheSwitchStatement.htm)

switch (expression)
{
case constant-expression:
statement
jump-statement
[default:
statement
jump-statement]
}


pozdrawiam
yes, thank you for your "RTFM" and your copy-paste from msdn...i can
read the documentation too...and I know how to use a switch
statement...what I was looking for, if you had bothered to read my
message at all, was a way to replicate ONE PARTICULAR ASPECT of the VB
select Case statement that I could not seem to replicate in a C# Switch
statement, i.e., evaluating an expression, rather than a constant, for
truth.
 
Hi Scott,

I'm afraid that you cannot do that.

If you look at the definition of the switch statement in C#:

switch (expression)
{
case constant-expression:
statement
jump-statement
[default:
statement
jump-statement]
}


you see that the case expression have to be an constant-expression, you
cannot use an expression there. also the expression of the switch clause
must be either integer or string.
Also the jump-statement( break,continue) is a MUST therefore is not
possible to have explicit fall through as in C++.

Hope this help,
 
Ignacio said:
Hi Scott,

I'm afraid that you cannot do that.

If you look at the definition of the switch statement in C#:

switch (expression)
{
case constant-expression:
statement
jump-statement
[default:
statement
jump-statement]
}


you see that the case expression have to be an constant-expression, you
cannot use an expression there. also the expression of the switch clause
must be either integer or string.
Also the jump-statement( break,continue) is a MUST therefore is not
possible to have explicit fall through as in C++.

Hope this help,
well...it helps in that at least I know for sure that it can't be done.
I thought maybe someone knew of a way to make it work. Oh well...

So would you say the proper way to do it then would be:
if(x>y)
{}
if(y<x)
{}
if(x=5)
{}

that seems like a lot of annoying ifs...at least they're not nested though.

thanks for the help.
 
Scott,
I have found no way to do the same or similar using Switch. Is there any?
Unfortunately (or is it fortunately) there no equivalent switch in C#.

Try searching this newsgroup at groups.google.com there have been a couple
of discussions in the last month or two on why it should or should not be
supported...

I would suggest an if/else if/else if/else construct.

Hope this helps
Jay
 
yes, thank you for your "RTFM" and your copy-paste from msdn...i can
read the documentation too...and I know how to use a switch
statement...what I was looking for, if you had bothered to read my
message at all, was a way to replicate ONE PARTICULAR ASPECT of the VB
select Case statement that I could not seem to replicate in a C# Switch
statement, i.e., evaluating an expression, rather than a constant, for
truth.
it's 'cause C# is similar to C++ more then VB6 :)

SELECT CASE True
case x > y:
dosomestuff()
case x = 5:
dosomestuff()
case y > x:
dosomestuff()
END SELECT

use if statment
there is no other way
f.e:

if(x>y)
{
robcstam();
}
else
{
if(x=5)
{
robcostam();
}
else
{
if(y>x)
{
robcosinnego()
}
}
}

pozdrawiam
 
Jay said:
Scott,


Unfortunately (or is it fortunately) there no equivalent switch in C#.
Well, I don't know if it's unfortunate or fortunate...I've been of the
belief that most of the things not in C# that we could do in VB were
fortunate, because we get forced to do things "the right way". Given
everything else I can do in C#...i'll live without this.

thanks.
 
So would you say the proper way to do it then would be:
if(x>y)
{ function0 }
if(y<x)
{ function1 }
if(x=5)
{ function2 }
no because
if x =5 and x>y
your aplication use function0 and function2
what is not you point
see my post bellow
 
Ich think you're wrong:

Dim d&
d = 10

Select Case d
Case Is < 20
Debug.Print "<20"
Case Is > 5
Debug.Print ">5"
End Select


This VB6 Code prints "<20". If the first condition is true, the others are
not evaluated

mfg GP
 
Günter Prossliner said:
Ich think you're wrong:

Dim d&
d = 10

Select Case d
Case Is < 20
Debug.Print "<20"
Case Is > 5
Debug.Print ">5"
End Select


This VB6 Code prints "<20". If the first condition is true, the others are
not evaluated

mfg GP

Try:

Dim d
d = 100
Dim c
c = "hi"

Select Case True
Case d < 20:
Debug.Print "<20"
Case d = 10:
Debug.Print "==10"
Case c = "hi":
Debug.Print "hi"
End Select

Select Case True (as opposed to "Select Case variable" evaluates every
case until it comes to the first true one, and each case expression can
be basically anything that evaluates to true or false. in this case we
can evaluate different expressions for different variables or objects
with the same select case construct, whereas in c#, you have to specify
an expression you want to evaluate (like Switch(d) and then switch(c)
separately)

Perhaps my original example was poor. Sorry.
 
Select Case True (as opposed to "Select Case variable" evaluates every
case until it comes to the first true one, and each case expression can
be basically anything that evaluates to true or false. ...

Ok, the behavior is still the same. After the first expression if evaluated
true, the rest will not take affect.

So the C# of

SELECT CASE True
case x > y:
Debug.Print "1"
case x = 5:
Debug.Print "2"
case y > x:
Debug.Print "3"
END SELECT


would be:

if(x>=y)
{Console.WriteLine("1");}
else if(y=<x)
{Console.WriteLine("2");}
else if(x=5)
{Console.WriteLine("3");}

NOT

if(x>=y)
{Console.WriteLine("1");}
if(y<=x)
{Console.WriteLine("2");}
if(x=5)
{Console.WriteLine("3");}

Assuming x==5, y ==5

Result 1:
1

Result 2:
1
2
3

(I've replaced < / > with <= / >= to demonstrate the difference!)

Why don't you like the ifs? IMO the code is better readable than the VB
code.

GP
 
Back
Top