1

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 ?

3
  • 1
    Ask yourself - what does the bytesss += ... opcode do? Commented Oct 10, 2012 at 9:18
  • that should be a warning Commented Oct 10, 2012 at 9:25
  • 1
    @codingbiz, that is an error, not a warning Commented Oct 10, 2012 at 9:28

6 Answers 6

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Even when entering the loop, it would still be an uninitialized variable.
Thanks @Daniel Hilgarth, overlooked it due to +=, updated my answer
1

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());
}

Comments

0

string bytesss;

should be

string bytesss = String.Empty;

try that.

2 Comments

It's complaining because you didn't initialize bytesss, so it doesn't know what kind of initial value to give it.
you should edit your answer to include your additional statement, posting as a comment is usually not ideal
0

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.

Comments

0

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

Comments

0

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

string  bytesss = string.Empty;

or

string bytesss = "";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.