newbie
newbie

Reputation: 125

Why do I get the error "Use of Unassigned local Variable"?

I have this code below :

string mybytes(int[] sbytes)
{
    string bytesss;
    for(int i=1; i< sbytes.GetUpperBound(0)+1;i++)
    {
        bytesss += "," + IntToHex(sbytes[i - 1]).ToString();
    }
    return bytesss;
}

private string IntToHex(int number)
{
    return string.Format("{0:x}", number);
}

Why does it keep raising "Use of Unassigned local Variable" error ?

Upvotes: 1

Views: 1031

Answers (6)

craftworkgames
craftworkgames

Reputation: 9957

The problem here is that you're local variable called bytesss is not being initialized before you try to concatenate to it. Doing this is similar to trying to concatenate a string to null, it just doesn't make any sense. Consider the following code.

string bytesss = null;
bytesss += ",";

or to put it another way...

string bytesss = null + ",";

Neither of these things make sense, so you need to make sure bytesss is set to some initial value before trying to concatenate. For example:

string bytesss = "";

or

string bytesss = string.Empty;

Lastly, there's a few other things you can do to make your code easier. If you're using a recent version of the .NET framework and have LINQ you can do something like this:

string mybytes(int[] sbytes)
{ 
    return string.Join(",", sbytes.Select(i => IntToHex(i)).ToArray());
}

or if you're using an older version you could do something like this:

string mybytes(int[] sbytes)
{ 
    List<string> list = new List<string>();

    foreach(int i in sbytes)
        list.Add(IntToHex(i));

    return string.Join(",", list.ToArray());
}

Upvotes: 1

Adil
Adil

Reputation: 148120

C# compiler wants you to provide value to string variable before you use it, What if it does not go in the for loop as compiler does not know whether control will go in for loop during execution. If control does not go in for loop method will return unassigned variable which compiler does not like and gives you error. You are using the value of variable before assigning it due to += operator so you will get error due to this over here. Assigning empty string could prevent us from compilation error as statement given below assigns empty string.

string  bytesss = string.Empty;

Upvotes: 1

user1734113
user1734113

Reputation:

Use the following in your code. It solves the issue.

string  bytesss = string.Empty;

or

string bytesss = "";

Upvotes: 0

HTTP 410
HTTP 410

Reputation: 17608

You've declared the string without initialising it. Try declaring it as "string bytess = String.Empty;" instead. This essay is a good starting point.

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

Well, you never initialize bytesss in your code.

Change it to this:

string bytesss = "";

There are actually two problems in your code:

  1. If sbytes.GetUpperBound(0) returns 0, your loop will never be entered, so bytesss += ... will never be executed.
  2. Even when the loop is entered, you still have a problem. bytesss += ... is equivalent to bytesss = bytesss + .... But what is bytesss in the first iteration of your loop? It is undefined, because it never has been initialized.

Upvotes: 0

HypnoToad
HypnoToad

Reputation: 605

string bytesss;

should be

string bytesss = String.Empty;

try that.

Upvotes: 0

Related Questions