Local variable and Try/Catch
        Posted  
        
            by 
                3D-kreativ
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by 3D-kreativ
        
        
        
        Published on 2012-07-06T08:58:27Z
        Indexed on 
            2012/07/06
            9:15 UTC
        
        
        Read the original article
        Hit count: 202
        
c#
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!";
        }
        © Stack Overflow or respective owner