CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  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.

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