switch(true) in c#

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

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
 
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
 
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 said:
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 said:
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?
 
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 said:
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 said:
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 said:
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?
 
sorry, that was meant to say "googled this", as in, I got that from google


Grant Merwitz said:
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 said:
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 said:
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
-------------------------------------------------------------------------- -----


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?
 
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 said:
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 said:
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 said:
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
------------------------------------------------------------------------- -
 
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 said:
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 said:
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 said:
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
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
------------------------------------------------------------------------- -
-----


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?
 
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/d...-us/csref/html/vcoriCProgrammersReference.asp.

The switch statement
(http://msdn.microsoft.com/library/d.../en-us/csref/html/vclrftheswitchstatement.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/default.asp?url=/library/en-us/csref/html/vclrftheifstatement.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 said:
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 said:
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 said:
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?
 
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,

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top