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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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"?
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.
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".
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.
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.
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'