Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unassigned local variable

Posted on 2007-03-28
5
Medium Priority
?
628 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 3

Accepted Solution

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

Assisted Solution

by:gregoryyoung
gregoryyoung earned 240 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 240 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 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