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

x
?
Solved

try {} assignment

Posted on 2004-11-02
2
Medium Priority
?
219 Views
Last Modified: 2010-04-15
Hi,
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;
try{a=some_function();}
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?
0
Comment
Question by:deniX23
[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
2 Comments
 
LVL 8

Accepted Solution

by:
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
0
 
LVL 9

Expert Comment

by:noulouk
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:
try{
int a;
a=some_function();
MessageBox.Show(a.ToString ());              ----Or myfunction() to do what I want.
}
catch(Exception e)
{<some code>}

Hope this will help you.
0

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