473,394 Members | 1,714 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,394 software developers and data experts.

switch(true) in c#

Hi,

In classic ASP with vbscript, I'd sometimes do this.

Select case true
case x = 3
code
case y = 6
code
case r = "tick"
code
End select

In C#, is there a way of doing such a thing? The current code I have that
is /not/ valid is this.

switch(true) {
case sPN!="":
something();
break;
case sPF!="":
//something else
break;
default:
//do something different
break;
}

I know that I could do a few nested ifs, but if there are 10 different
unrelated conditions I want to test for, it could get kinda messy.

Thanks,

Frank
Nov 19 '05 #1
9 1508
switch statements evaluate strings, they don't work on boolean variables.
have a look at the syntax for it in the helpfile and work your code
accordingly

--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc
-------------------------------------------------------------------------------
"Frank" <frank.favinger@goooooogle's e-mail domain> wrote in message
news:U7********************@giganews.com...
Hi,

In classic ASP with vbscript, I'd sometimes do this.

Select case true
case x = 3
code
case y = 6
code
case r = "tick"
code
End select

In C#, is there a way of doing such a thing? The current code I have that
is /not/ valid is this.

switch(true) {
case sPN!="":
something();
break;
case sPF!="":
//something else
break;
default:
//do something different
break;
}

I know that I could do a few nested ifs, but if there are 10 different
unrelated conditions I want to test for, it could get kinda messy.

Thanks,

Frank

Nov 19 '05 #2
Thanks Alvin. I wish I had a help file or the C# equivalent to the WSH
documentation. In Visual Studio, F1 just gives me

Help Is Not Installed for Visual Studio

I'll see if we have MSDN where I am.

Thanks

"Alvin Bruney [Microsoft MVP]" <www.lulu.com/owc> wrote in message
news:ud**************@TK2MSFTNGP14.phx.gbl...
switch statements evaluate strings, they don't work on boolean variables.
have a look at the syntax for it in the helpfile and work your code
accordingly

--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc
-------------------------------------------------------------------------- -----

"Frank" <frank.favinger@goooooogle's e-mail domain> wrote in message
news:U7********************@giganews.com...
Hi,

In classic ASP with vbscript, I'd sometimes do this.

Select case true
case x = 3
code
case y = 6
code
case r = "tick"
code
End select

In C#, is there a way of doing such a thing?

Nov 19 '05 #3
google this, maybe it can help

http://cplus.about.com/library/weekly/aa022205a.htm
but basis, which you seem to more or less have is

switch(variable)
{
case "value" :
//do something
break;
case "othervalue":
//do something else
break;
default:
//do default thing
break;
}

"Frank" <frank.favinger@goooooogle's e-mail domain> wrote in message
news:Rd*****************************************@g iganews.com...
Thanks Alvin. I wish I had a help file or the C# equivalent to the WSH
documentation. In Visual Studio, F1 just gives me

Help Is Not Installed for Visual Studio

I'll see if we have MSDN where I am.

Thanks

"Alvin Bruney [Microsoft MVP]" <www.lulu.com/owc> wrote in message
news:ud**************@TK2MSFTNGP14.phx.gbl...
switch statements evaluate strings, they don't work on boolean variables.
have a look at the syntax for it in the helpfile and work your code
accordingly

--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc
--------------------------------------------------------------------------

-----


"Frank" <frank.favinger@goooooogle's e-mail domain> wrote in message
news:U7********************@giganews.com...
> Hi,
>
> In classic ASP with vbscript, I'd sometimes do this.
>
> Select case true
> case x = 3
> code
> case y = 6
> code
> case r = "tick"
> code
> End select
>
> In C#, is there a way of doing such a thing?


Nov 19 '05 #4
sorry, that was meant to say "googled this", as in, I got that from google
"Grant Merwitz" <gr***@workshare.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
google this, maybe it can help

http://cplus.about.com/library/weekly/aa022205a.htm
but basis, which you seem to more or less have is

switch(variable)
{
case "value" :
//do something
break;
case "othervalue":
//do something else
break;
default:
//do default thing
break;
}

"Frank" <frank.favinger@goooooogle's e-mail domain> wrote in message
news:Rd*****************************************@g iganews.com...
Thanks Alvin. I wish I had a help file or the C# equivalent to the WSH
documentation. In Visual Studio, F1 just gives me

Help Is Not Installed for Visual Studio

I'll see if we have MSDN where I am.

Thanks

"Alvin Bruney [Microsoft MVP]" <www.lulu.com/owc> wrote in message
news:ud**************@TK2MSFTNGP14.phx.gbl...
switch statements evaluate strings, they don't work on boolean
variables.
have a look at the syntax for it in the helpfile and work your code
accordingly

--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc
--------------------------------------------------------------------------

-----


"Frank" <frank.favinger@goooooogle's e-mail domain> wrote in message
news:U7********************@giganews.com...
> Hi,
>
> In classic ASP with vbscript, I'd sometimes do this.
>
> Select case true
> case x = 3
> code
> case y = 6
> code
> case r = "tick"
> code
> End select
>
> In C#, is there a way of doing such a thing?



Nov 19 '05 #5
That's just it. I'm not wanting to switch based on the value of one
variable. I am wanting to execute based on the first true condition of a
bunch of different conditions. But thanks.

Frank

"Grant Merwitz" <gr***@workshare.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
google this, maybe it can help

http://cplus.about.com/library/weekly/aa022205a.htm
but basis, which you seem to more or less have is

switch(variable)
{
case "value" :
//do something
break;
case "othervalue":
//do something else
break;
default:
//do default thing
break;
}

"Frank" <frank.favinger@goooooogle's e-mail domain> wrote in message
news:Rd*****************************************@g iganews.com...
Thanks Alvin. I wish I had a help file or the C# equivalent to the WSH
documentation. In Visual Studio, F1 just gives me

Help Is Not Installed for Visual Studio

I'll see if we have MSDN where I am.

Thanks

"Alvin Bruney [Microsoft MVP]" <www.lulu.com/owc> wrote in message
news:ud**************@TK2MSFTNGP14.phx.gbl...
switch statements evaluate strings, they don't work on boolean variables. have a look at the syntax for it in the helpfile and work your code
accordingly

--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc
-------------------------------------------------------------------------

- -----


"Frank" <frank.favinger@goooooogle's e-mail domain> wrote in message
news:U7********************@giganews.com...
> Hi,
>
> In classic ASP with vbscript, I'd sometimes do this.
>
> Select case true
> case x = 3
> code
> case y = 6
> code
> case r = "tick"
> code
> End select
>
> In C#, is there a way of doing such a thing?



Nov 19 '05 #6
javascript is interpreted, and is a very 'anything goes' kind of language.
It let's you do many things, you could never do in C# or VB.NET.

"Frank" <frank.favinger@goooooogle's e-mail domain> wrote in message
news:oM********************@giganews.com...
That's just it. I'm not wanting to switch based on the value of one
variable. I am wanting to execute based on the first true condition of a
bunch of different conditions. But thanks.

Frank

"Grant Merwitz" <gr***@workshare.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
google this, maybe it can help

http://cplus.about.com/library/weekly/aa022205a.htm
but basis, which you seem to more or less have is

switch(variable)
{
case "value" :
//do something
break;
case "othervalue":
//do something else
break;
default:
//do default thing
break;
}

"Frank" <frank.favinger@goooooogle's e-mail domain> wrote in message
news:Rd*****************************************@g iganews.com...
> Thanks Alvin. I wish I had a help file or the C# equivalent to the WSH
> documentation. In Visual Studio, F1 just gives me
>
> Help Is Not Installed for Visual Studio
>
> I'll see if we have MSDN where I am.
>
> Thanks
>
> "Alvin Bruney [Microsoft MVP]" <www.lulu.com/owc> wrote in message
> news:ud**************@TK2MSFTNGP14.phx.gbl...
>> switch statements evaluate strings, they don't work on boolean variables. >> have a look at the syntax for it in the helpfile and work your code
>> accordingly
>>
>> --
>> Regards
>> Alvin Bruney
>> [Shameless Author Plug]
>> The Microsoft Office Web Components Black Book with .NET
>> available at www.lulu.com/owc, Amazon, B&H etc

------------------------------------------------------------------------- - > -----
>>
>>
>> "Frank" <frank.favinger@goooooogle's e-mail domain> wrote in message
>> news:U7********************@giganews.com...
>> > Hi,
>> >
>> > In classic ASP with vbscript, I'd sometimes do this.
>> >
>> > Select case true
>> > case x = 3
>> > code
>> > case y = 6
>> > code
>> > case r = "tick"
>> > code
>> > End select
>> >
>> > In C#, is there a way of doing such a thing?
>
>



Nov 19 '05 #7
At one time I was running VS w/o MSDN. What a pain. Luckily the language
reference can be found at
http://msdn.microsoft.com/library/de...sReference.asp.

The switch statement
(http://msdn.microsoft.com/library/de...hstatement.asp)
is basically for conditionally executing code when the value of the
expression provided matches (is equal to) the condition specified for the
case.

The if statement
(http://msdn.microsoft.com/library/de...fstatement.asp)
is better suited to what you're looking for since it is not restricted to a
single value.

HTH
----
http://www.davefancher.com
"Frank" wrote:
Thanks Alvin. I wish I had a help file or the C# equivalent to the WSH
documentation. In Visual Studio, F1 just gives me

Help Is Not Installed for Visual Studio

I'll see if we have MSDN where I am.

Thanks

"Alvin Bruney [Microsoft MVP]" <www.lulu.com/owc> wrote in message
news:ud**************@TK2MSFTNGP14.phx.gbl...
switch statements evaluate strings, they don't work on boolean variables.
have a look at the syntax for it in the helpfile and work your code
accordingly

--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc
--------------------------------------------------------------------------

-----


"Frank" <frank.favinger@goooooogle's e-mail domain> wrote in message
news:U7********************@giganews.com...
Hi,

In classic ASP with vbscript, I'd sometimes do this.

Select case true
case x = 3
code
case y = 6
code
case r = "tick"
code
End select

In C#, is there a way of doing such a thing?


Nov 19 '05 #8
None of your respondants are empathetic. Much as I disliked VB, the "select
case true" construct rocks. It is especially useful for filtering out error
conditions. Don't know of any equivalent.
Concerning help, I always use the online reference; it's always up to date.
Here's the link for C#: http://tinyurl.com/sb48

"Frank" wrote:
Hi,

In classic ASP with vbscript, I'd sometimes do this.

Select case true
case x = 3
code
case y = 6
code
case r = "tick"
code
End select

In C#, is there a way of doing such a thing? The current code I have that
is /not/ valid is this.

switch(true) {
case sPN!="":
something();
break;
case sPF!="":
//something else
break;
default:
//do something different
break;
}

I know that I could do a few nested ifs, but if there are 10 different
unrelated conditions I want to test for, it could get kinda messy.

Thanks,

Frank

Nov 19 '05 #9

Frank,

Here's an option for you. You'll need to decide if this is better than
multiple if/else statements.

int testInt = 0;

testInt = ("a" == "b") ? 1 : testInt;
testInt = ("a" == "c") ? 2 : testInt;
testInt = ("a" == "a") ? 3 : testInt;
testInt = ("a" == "d") ? 4 : testInt;

string resultString;
switch (testInt) {
case 1:
resultString = "1";
break;
case 2:
resultString = "2";
break;
case 3:
resultString = "3";
break;
case 4:
resultString = "4";
break;
default:
resultString = "";
break;
}

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #10

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

Similar topics

4
by: Robert Scheer | last post by:
Hi. I am trying to use switch this way, without success: //thedata is a numeric variable switch (thedata) { case < 10: ... case < 20: ...
6
by: Aristotelis E. Charalampakis | last post by:
Hi all, this is a newbie question :-) I was wondering if there was a way to use the switch statement in a manner that each case statement includes more that a simple value. i.e.: switch (...
21
by: Andy | last post by:
Can someone tell me if the following Switch...Case construct is valid? I'm wanting to check for multiple values in the Case statement without explicitly listing each values. So for example, will...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
9
by: Eric S. | last post by:
Why doesn't something like this work? I get "a constant value is expected" on all 3 case statements. private DataGrid CurrentDataGrid(DataGrid firstDg, DataGrid secondDg) { try { switch...
24
by: Mark | last post by:
Hi - how can I get a switch statement to look for a range of values? I have: function payCalc(field, field2) switch(field.value) { case 0-50: field2.value="lower band"; case 51-99:...
9
by: Raj | last post by:
public static void HandleException(ref Exception io_exException, bool i_blnPropagateException) { switch (true) { case io_exException is ApplicationHandledException: { if...
9
by: wouter | last post by:
hey hi..... I wanna make a switch wich does this: if pagid is set do A, if catid is set do B, if projectid is set do C, else do D. So i was thinking something like this:
2
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name':...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.