CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2003
    Location
    Ft. Worth Texas
    Posts
    31

    How to use an array variable as a case parameter

    Good morning all!

    VS2010
    .NET 3.5

    Here is a snippet in VB.Net that works, but the C# version does not.
    VB.Net:
    Code:
                  
    Dim a(8) As String
    Dim b As String
    Dim c As Short
    		
        a(0) = "There"
        a(1) = "Not Here"
        a(2) = "Over Here"
    		
        b = "Not Here"
        Select Case b
            Case a(0)
                c = 1
            Case a(1)
                c = 2
            Case a(2)
                c = 3
        End Select
    The C# code:

    The Case a[x]: is the problem.
    I'm getting an error: "A constant value is expected".
    Code:
    string a = new string[8];
    string b;
    int c;
    		
        a[0] = "There";
        a[1] = "Not Here";
        a[2] = "Over Here";
    		
        b = "Not Here";
    
    switch (b)
    {
        case a[0]:
            c = 1;
            break;
        case a[1]:
            c = 2;
            break;
        case a[2]:
            c = 3;
            break;
    }
    Can anyone help?
    Last edited by KKW; February 6th, 2012 at 10:32 AM.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to use an array variable as a case parameter

    Yup, the switch only allows constant values. If it did not then it would not be nearly as performant as currently the compiler can use a jump list. I'm not a VB guy, but if VB allows such thing that is a questionable decision IMO. You can already use if() { } else if() { } for this situation, so... use that.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to use an array variable as a case parameter

    Yes Vb seems to treat a select case block as though it were a group of If .. ElseIf. It is easier to read in the editor that a group of If elseif but I think the result is about the same.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Feb 2003
    Location
    Ft. Worth Texas
    Posts
    31

    Re: How to use an array variable as a case parameter

    Thanks,

    I will conform!
    I mean, I will make the changes using the If..ElseIf statements!

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: How to use an array variable as a case parameter

    Alternatively you could define an enum; see MSDN link.

    DataMiser, I think the switch statement performs substantially better than If...Elseif...Else...End constructs due to some compiler optimizations. Best case, it's a jump table; worst case its if...elseif...end. I think it's been discussed elsewhere on the forum (and on StackOverflow). I can find the reference if you're interested.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Jan 2010
    Posts
    1,133

    Re: How to use an array variable as a case parameter

    Note: A bit off-topic, sry.
    @DataMiser & BioPhysEngr: Are you both taking about VB.NET?
    If I understood DataMiser correctly, when the Select Case statement is used like that in VB, then it's treated as syntactic sugar for If...Else If...ElseIf...?
    Last edited by TheGreatCthulhu; February 11th, 2012 at 11:57 AM.

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to use an array variable as a case parameter

    I think BioPhysEngr was referring to C# there is no switch in VB. At least not that I am aware of.

    I was referring to the Select Case in VB behaving like the If ElseIf block. As you say kind of a syntactic sugar.

    For example when a case within a select case statement evaluates to true the code within the case is executed and the code exits the select case block just as it would if you build a big block of If ElseIf whereas in C# you must add the break; to get it to exit.

    I have not actually tested the performance of Select case compared to If ElseIf but I would not be surprised if the compiled code were exactly the same.
    Last edited by DataMiser; February 11th, 2012 at 12:59 PM.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured