Switch Question
-
Hi All, I am converting some of my vb.net code to c# and have run into a problem.
Select Case True Case p.ParameterType Is GetType(String) Response.Write("paramater is a string") End Select
the code above works but when I try to convert to c# I get the error: A constant value is expected.switch (true) { case pInfo.ParameterType == typeof(String): Response.Write("is type of string"); break; }
Is there any way to do this in c#? thanks tom -
Hi All, I am converting some of my vb.net code to c# and have run into a problem.
Select Case True Case p.ParameterType Is GetType(String) Response.Write("paramater is a string") End Select
the code above works but when I try to convert to c# I get the error: A constant value is expected.switch (true) { case pInfo.ParameterType == typeof(String): Response.Write("is type of string"); break; }
Is there any way to do this in c#? thanks tom -
Hi All, I am converting some of my vb.net code to c# and have run into a problem.
Select Case True Case p.ParameterType Is GetType(String) Response.Write("paramater is a string") End Select
the code above works but when I try to convert to c# I get the error: A constant value is expected.switch (true) { case pInfo.ParameterType == typeof(String): Response.Write("is type of string"); break; }
Is there any way to do this in c#? thanks tomThe variable goes in the
switch
part, the constants go in thecase
part.switch(pInfo.ParameterType)
{
case typeof(string):
// Do stuff
break;
}Although I'm not sure how it handles
Type
s. I've only ever used a switch statement for numbers, strings and enumerators.
Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
-
The variable goes in the
switch
part, the constants go in thecase
part.switch(pInfo.ParameterType)
{
case typeof(string):
// Do stuff
break;
}Although I'm not sure how it handles
Type
s. I've only ever used a switch statement for numbers, strings and enumerators.
Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
Thanks for the reply. I tried that but then I get the error: A value of an integral type expected this is the only work around that I can think of, but it isnt really an ideal solution: switch (pInfo.ParameterType.Name) { case "String": Response.Write("is type of string"); break; } I cant believe you cant do this in c#! thanks
-
Thanks for the reply. I tried that but then I get the error: A value of an integral type expected this is the only work around that I can think of, but it isnt really an ideal solution: switch (pInfo.ParameterType.Name) { case "String": Response.Write("is type of string"); break; } I cant believe you cant do this in c#! thanks
mcd2424 wrote:
I cant believe you cant do this in c#!
I would imagine that C# is enforcing some additional restrictions in order to produce optimised code - something VB is trading for ease of coding.
Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
-
Hi All, I am converting some of my vb.net code to c# and have run into a problem.
Select Case True Case p.ParameterType Is GetType(String) Response.Write("paramater is a string") End Select
the code above works but when I try to convert to c# I get the error: A constant value is expected.switch (true) { case pInfo.ParameterType == typeof(String): Response.Write("is type of string"); break; }
Is there any way to do this in c#? thanks tomHi, the switch statement isn't designed for such a thing. Use if-then-else-if instead:
if (pInfo.ParameterType == typeof(String)) {
Response.Write("is type of string");
} else if (...) {
} else if (...) {
}Robert
-
Hi, the switch statement isn't designed for such a thing. Use if-then-else-if instead:
if (pInfo.ParameterType == typeof(String)) {
Response.Write("is type of string");
} else if (...) {
} else if (...) {
}Robert
Robert Rohde wrote:
the switch statement isn't designed for such a thing.
Please explain
only two letters away from being an asset
-
Robert Rohde wrote:
the switch statement isn't designed for such a thing.
Please explain
only two letters away from being an asset
Hi, this simply means it can only work with values which are internally represented by integers. This applies to integer types, boolean and enumeration values. Don't ask me why MS didn't extend its capabilities - its just a fact. Robert
-
Hi, this simply means it can only work with values which are internally represented by integers. This applies to integer types, boolean and enumeration values. Don't ask me why MS didn't extend its capabilities - its just a fact. Robert
Robert Rohde wrote:
can only work with values which are internally represented by integers
Then I must be doing something wrong for this to work :rolleyes:
string MyString = "Foo"; switch(MyString) { case "Foo": break; case "Bar": break; default: break; }
only two letters away from being an asset
-
Robert Rohde wrote:
can only work with values which are internally represented by integers
Then I must be doing something wrong for this to work :rolleyes:
string MyString = "Foo"; switch(MyString) { case "Foo": break; case "Bar": break; default: break; }
only two letters away from being an asset
Hi, as usual
string
seems to get a special treatment :). Probably there are some more case where switch works, but the fact remains that its rather limited (compared to some other languages). Robert