Solved

Unassigned local variable

Posted on 2007-03-28
5
624 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 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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 Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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