Solved

Unassigned local variable

Posted on 2007-03-28
5
619 Views
Last Modified: 2008-01-09
public static String toAscii(string hexString)
{
string binaryString;
for (int i=0;i<hexString.Length;i=i+2)
{
string tmpstr = (((char)hexString[i]).ToString()+((char)hexString[i+1]).ToString());
int hex = Convert.ToInt16(tmpstr,16);
binaryString += (char)hex;
}
return binaryString;
}


What is the problem with this code it is saying unassigned local variable binaryString...???
0
Comment
Question by:Rahamathulla_J
  • 3
5 Comments
 
LVL 3

Accepted Solution

by:
el_dios earned 90 total points
ID: 18813788
use string binaryString=null;
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 80 total points
ID: 18813996
I'd probably use ...

string binaryString = String.Empty;

because you are appending to it and it is a bit clearer but 6 vs a half dozen.
0
 
LVL 3

Expert Comment

by:el_dios
ID: 18814009
I would agree, see the problem is the compiler doesn't know whether the for loop would be executed or not, it may and probably with your logic it will, but compiler can't guarentee it, so the string has to be initialized to some value..
0
 
LVL 12

Assisted Solution

by:andrewjb
andrewjb earned 80 total points
ID: 18814420
Even if the for loop does get executed, the first iteration tries to do

binaryString += ....

where binaryString hasn't been initialised.

So binaryString = String.Empty (and not = null) at the beginning is required.
0
 
LVL 3

Expert Comment

by:el_dios
ID: 18814444
agreed
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now