C# switch vs. VB Select Case

G

Guest

I am a convert from VB to C# so bear with me on this "conversion" question

C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. Now I find myself wanting to use "Select Case" i.e., "switch" in C# regularly, but I always have to find another way b/c C#'s switch statement only allows static or integral variables. For example, I often want to use a switch statement based on the value that is returned in a database field, but you can't do this - you get the 'ol C# error "A value of integral type expected

I am sure I am missing something here, but I just can't seem to find an easy, consistent "workaround" in C# for the way in am comfortable programming in VB. I am sure this is something I am doing wrong in my "structure" and I guess my real question is what is the most efficient, and fast method in C# of testing for mutliple various results of a dynamic variable (like the value of a database field)?
 
D

DalePres

The basic effect of what you're talking about is that switch just cannot
parse code in the switch parameter. The parameter can be a variable but it
can't be a variable with some operation applied. So the key is to just
retrieve your data and process it into a variable then use the variable in
the switch.

Dale

pgraeve said:
I am a convert from VB to C# so bear with me on this "conversion" question.

C# switch statement seems to be the closest relative to VB's Select Case.
I used VB's Select Case statement liberally. Now I find myself wanting to
use "Select Case" i.e., "switch" in C# regularly, but I always have to find
another way b/c C#'s switch statement only allows static or integral
variables. For example, I often want to use a switch statement based on the
value that is returned in a database field, but you can't do this - you get
the 'ol C# error "A value of integral type expected"
I am sure I am missing something here, but I just can't seem to find an
easy, consistent "workaround" in C# for the way in am comfortable
programming in VB. I am sure this is something I am doing wrong in my
"structure" and I guess my real question is what is the most efficient, and
fast method in C# of testing for mutliple various results of a dynamic
variable (like the value of a database field)?
 
M

Michael Sparks

Rearranged.

DalePres said:
Case.
I used VB's Select Case statement liberally. Now I find myself wanting to
use "Select Case" i.e., "switch" in C# regularly, but I always have to find
another way b/c C#'s switch statement only allows static or integral
variables. For example, I often want to use a switch statement based on the
value that is returned in a database field, but you can't do this - you get
the 'ol C# error "A value of integral type expected"
easy, consistent "workaround" in C# for the way in am comfortable
programming in VB. I am sure this is something I am doing wrong in my
"structure" and I guess my real question is what is the most efficient, and
fast method in C# of testing for mutliple various results of a dynamic
variable (like the value of a database field)?

The basic effect of what you're talking about is that switch just cannot
parse code in the switch parameter. The parameter can be a variable but it
can't be a variable with some operation applied. So the key is to just
retrieve your data and process it into a variable then use the variable in
the switch.

Take the following code:

switch(a)
{
case b:
break;
}

In this code, 'a' can be an expression. That is, you could put something
like 'var1+var2', or the result of a database lookup, or any other
expression you like.
The 'a' is evaluated once at the top of the switch. The catch is, 'a' must
evaluate to an integer or a string. This is different from VB where you
could specify any manner of different data types.

The 'b' can also be an expression, as long as it can be evaluated at compile
time - for example, you could specify '1+2' as the 'b'. You can't, however,
specify 'var1+var2' for 'b'.

Addressing the OP's problem, you might try casting the database field to its
primitive type before using it in the switch. Otherwise, you will likely be
trying to perform a switch using an object type, which of course isn't an
integer or a string. For example:

SqlDataReader reader=(magical code goes here)
switch((int)reader["mycolumn"])
{
case 1:
break;
case 2:
break;
}

HTH.
 
J

Jeffrey Tan[MSFT]

Hi pkg,

Is your problem resolved?

If you still have any concern, please feel free to tell me, I will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Top