C# switch vs. VB Select Case

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pgraeve

    C# switch vs. VB Select Case

    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)?
  • DalePres

    #2
    Re: C# switch vs. VB Select Case

    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" <pkg@pkgi.net > wrote in message
    news:0485A1C2-71DF-4344-929A-78CB03815B84@mi crosoft.com...[color=blue]
    > I am a convert from VB to C# so bear with me on this "conversion "[/color]
    question.[color=blue]
    >
    > C# switch statement seems to be the closest relative to VB's Select Case.[/color]
    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"[color=blue]
    >
    > I am sure I am missing something here, but I just can't seem to find an[/color]
    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)?


    Comment

    • Michael Sparks

      #3
      Re: C# switch vs. VB Select Case

      Rearranged.

      "DalePres" <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote in message
      news:uNirUWo7DH A.2404@TK2MSFTN GP11.phx.gbl...[color=blue]
      > "pgraeve" <pkg@pkgi.net > wrote in message
      > news:0485A1C2-71DF-4344-929A-78CB03815B84@mi crosoft.com...[color=green]
      > > C# switch statement seems to be the closest relative to VB's Select[/color][/color]
      Case.[color=blue]
      > 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[/color]
      find[color=blue]
      > 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[/color]
      the[color=blue]
      > value that is returned in a database field, but you can't do this - you[/color]
      get[color=blue]
      > the 'ol C# error "A value of integral type expected"[color=green]
      > >
      > > I am sure I am missing something here, but I just can't seem to find an[/color]
      > 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,[/color]
      and[color=blue]
      > 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[/color]
      it[color=blue]
      > 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.[/color]

      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)rea der["mycolumn"])
      {
      case 1:
      break;
      case 2:
      break;
      }

      HTH.


      Comment

      • Jeffrey Tan[MSFT]

        #4
        RE: C# switch vs. VB Select Case


        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.

        Comment

        Working...