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


try {} assignment

Posted on 2004-11-02
Medium Priority
Last Modified: 2010-04-15
How come when i do an assignment inside a try{} block and afterwards try to use the assigned variable it tells me I am using it without assigning?
Take this for example:
int a;
catch(Exception e)
{<some code>}
MessageBox.Show(a.ToString ());
I get the error message when compiling: Use of unassigned local variable 'a' (in the MessageBox line)

If i were to insert the line 'a=5' before the 'try' it would compile and work fine.
Am i missing something here?
Question by:deniX23
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

Accepted Solution

razo earned 2000 total points
ID: 12471561
because the program takes into account the possibilty that the code would enter the catck block instead of the try block and in that instance the int a is not assigned to any value

Expert Comment

ID: 12471570
Hi denix23,

With int, you have to assigned a value when you declare the variable:
int a=0;
particulary if you use try {}.

Another way possible:
int a;
MessageBox.Show(a.ToString ());              ----Or myfunction() to do what I want.
catch(Exception e)
{<some code>}

Hope this will help you.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

604 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