简体   繁体   中英

Local variable and Try/Catch

I get an error, a red line below the variable intAge in the if-statement in the code. It says the variable is local, but how could it be local when it is declared in the beginning of the code? Does it have to do with the Try/Catch part? The reason why my code looks like it does, is just beacuse I have to use a Try/Catch in the code for this task. Preciate some suggestions to solve this in a similiar and correct way? Thanks!

int intAge;

        try
        {
            intAge = int.Parse(age);
        }
        catch (Exception)
        {
            MessageBox.Show("Enter an age in numbers!","Error!");
        }
        finally
        {

        }
        // Check input
        if (intAge < 1)
        {
            inputOk = false;
            errorMessage = "Please enter 1 or higher!";
        }

just initialize the intAge:

int intAge = 0;

You are getting error use of unassigned local variable .

Since you are assigning the value in the try block, the compiler can't determine if the assignment will take place or not (in case if int.Parse(age) throws an exception), and then in your check if(intAge<1) you are getting the error because you are using a variable not previously assigned.

Definite assignment - MSDN

At a given location in the executable code of a function member, a variable is said to be definitely assigned if the compiler can prove, by static flow analysis, that the variable has been automatically initialized or has been the target of at least one assignment.

If int.Parse fails, the intAge variable will not be initialized. You may initialize it at delecration

int intAge = 0;

You may avoid the double error :

    int intAge;

    if (!int.TryParse(age, out intAge))
    {
        inputOk = false;  
        errorMessage = "Enter an age in numbers!";  
    }
    else
    {
        // Check input  
        if (intAge < 1)  
        {  
            inputOk = false;  
            errorMessage = "Please enter 1 or higher!";  
        }
    }

It's local because it's declared in the local scope. Your code may be better structured like:

int intAge;

if (!int.TryParse(age, out intAge))
{
    MessageBox.Show(...
}
else
{
    if (intAge < 1)
        {
            inputOk = false;
            errorMessage = "Please enter 1 or higher!";
        }
}

With your code above you will display two errors, one for non-numeric, and then one for less than 1. The initial complaint of the compiler was because your integer was not guaranteed to be initialised.

The compiler complains that the local variable intAge might not have been initialized when used for the first time. This may happen when int.Parse(age) throws an exception. To correct this, just initialize intAge to some proper value.

The problem is age is not initialized initialize it to 0 , and try . it must work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM