Does "new" modifier changes the scope?

Junfeng Liang 156 Reputation points
2021-12-02T23:35:36.05+00:00

I am trying to pass a variable into the switch block, change it inside and then acess the variable outside the switch block. Here is the code I cannot understand:

//A method in a cloass
string SomeMethod(int num, int caseType)
{
string[] rValue;
switch (caseType)
{
Case 0:
rValue=new string[]{"a", "b","c"};
break;
defaut:
rValue=new string[]{"1", "2","3"};
Break;
}
return rValue[num];
}

An error showed up saying rValue is unassigned. What change the scope of "rValue". Is it because I use new to assign values for "rValue"?

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. WayneAKing 4,931 Reputation points
    2021-12-03T02:51:11.387+00:00

    It does not compile: CS0165 use of unassigned
    local variable 'rValue'

    You get that error because if catagory is not
    0 or 1 then rValue is never assigned a value
    before it is used in the return statement.

    • Wayne

5 additional answers

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2021-12-03T01:32:03.653+00:00

    string SomeMethod(int num, int caseType)
    {
    string[] rValue;
    switch (caseType)
    {
    Case 0:
    rValue=new string[]{"a", "b","c"};
    break;
    defaut:
    rValue=new string[]{"1", "2","3"};
    Break;
    }
    return rValue[num];
    }

    When posting code samples, please use the "Code Sample"
    feature in the forum editor. It's on the menu bar - the
    button with the binary digits, fifth from the left.

    Also use copy and paste to post the actual code you
    are using, don't try to type it all in again. That
    often leads to errors or omissions, such as in the
    code you posted. There you have "defaut" instead
    of "default" and "Break" instead of "break" and
    "Case" instead of "case".

    • Wayne
    0 comments No comments

  2. WayneAKing 4,931 Reputation points
    2021-12-03T01:38:38.553+00:00

    An error showed up saying rValue is unassigned.

    As a follow-up observation, it is probably the
    defaut: instead of default: that triggers an
    error for rValue in the return statement, if that
    is in your actual code.

    • Wayne
    0 comments No comments

  3. Junfeng Liang 156 Reputation points
    2021-12-03T02:16:11.88+00:00

    Sorry, my code is much longer than this. I just make a sample that is similar to my code. In my actual code, I did not use default. I did work around by putting return in each case. But as a beginner to C#, I am trying to figure what cause the failure to access the value I assign in the switch block.


  4. Junfeng Liang 156 Reputation points
    2021-12-03T02:39:17.913+00:00
    public string Num2String(int number, int catagory)
        {
            string[] rValue;
            switch (catagory)
            {
                case 0:
                    rValue= new string[4] { "a", "b", "c", "d" };
                    break;
    
                case 1:
                    rValue = new string[5] { "A","B", "C","D","E" };
    
                    break;
    
    
            }
            return rValue[number];
            }
    

    It does not compile: CS0165 use of unassigned local variable 'rValue'

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.