Solved

try {} assignment

Posted on 2004-11-02
215 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
Question by:deniX23
    2 Comments
     
    LVL 8

    Accepted Solution

    by:
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    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.
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…

    884 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

    21 Experts available now in Live!

    Get 1:1 Help Now