Solved

Unassigned local variable

Posted on 2007-03-28
5
618 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
Comment Utility
use string binaryString=null;
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 80 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
agreed
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

10 Experts available now in Live!

Get 1:1 Help Now