Solved

Unassigned local variable

Posted on 2007-03-28
5
622 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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