By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

C# switch vs. VB Select Case

P: n/a
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)?
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <pk*@pkgi.net> wrote in message
news:04**********************************@microsof t.com...
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)?
Nov 15 '05 #2

P: n/a
Rearranged.

"DalePres" <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote in message
news:uN**************@TK2MSFTNGP11.phx.gbl...
"pgraeve" <pk*@pkgi.net> wrote in message
news:04**********************************@microsof t.com...
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)?

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

P: n/a

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.

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.