473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Switch Statement Help

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?

Nov 15 '05 #1
11 2162
<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 - Przemek Sulikowski
Nov 15 '05 #2
Selvin wrote:
<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.

Nov 15 '05 #3
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 Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Scott C. Reynolds" <sr*********@hotmail.com> wrote in message
news:FH**********************@bgtnsc04-news.ops.worldnet.att.net...
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?

Nov 15 '05 #4
> 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

--
Selvin - Przemek Sulikowski
Nov 15 '05 #5
Ignacio Machin wrote:
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.

Nov 15 '05 #6
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

"Scott C. Reynolds" <sr*********@hotmail.com> wrote in message
news:FH**********************@bgtnsc04-news.ops.worldnet.att.net... 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?

Nov 15 '05 #7
Jay B. Harlow [MVP - Outlook] wrote:
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#.

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.
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

"Scott C. Reynolds" <sr*********@hotmail.com> wrote in message
news:FH**********************@bgtnsc04-news.ops.worldnet.att.net...
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?



Nov 15 '05 #8
> 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

--
Selvin - Przemek Sulikowski
Nov 15 '05 #9
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

"Scott C. Reynolds" <sr*********@hotmail.com> wrote in message
news:FH**********************@bgtnsc04-news.ops.worldnet.att.net...
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?

Nov 15 '05 #10
Günter Prossliner wrote:
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.
"Scott C. Reynolds" <sr*********@hotmail.com> wrote in message
news:FH**********************@bgtnsc04-news.ops.worldnet.att.net...
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?



Nov 15 '05 #11
> 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
Nov 15 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
19
by: rdavis7408 | last post by:
Hello, I have four textboxes that the user enters the price per gallon paid at the pump, the mileage per gallon and I would like to then calculate the cost per gallon and use a switch statement to...
2
by: Macca | last post by:
Hi, I have a switch statement that has 5+ case statements. Each of these case statements copies form one array to another. Rather than doing a separate try..catch statement for each case...
4
by: priyanka | last post by:
Hi there, I had a question. Is there any way of testing a string value in a switch statement. I have about 50 string values that can be in a string variable. I tried cheking them with the if...
5
by: Phuff | last post by:
Hey all, I need some direction help. I have a switch case statement that is seemingly my only option right now, but its too large and not easy to maintain the code. Here goes... I have part...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
3
by: Drowningwater | last post by:
I'm writing a program and I've made a switch statement. I have several quesions: 1. I have a string variable and when I try to use that string variable with the switch function I get a compiling...
2
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.