Jonathan Leigh
Jonathan Leigh

Reputation: 31

I declare a variable and cannot use it inside of the switch statement

string gr = comboBox1.ValueMember;
decimal sum = 0M;
try
{
    decimal rite = Convert.ToDecimal(textBox1.Text);
    decimal left = Convert.ToDecimal(textBox2.Text);
}
catch (Exception)
{
    string swr = "Please enter REAL a number, can be with decimals";
    label2.Text = swr;
}
switch (gr)
{
    case "X":

        sum = rite * left;
        break;
    case "/":

        break;
    case "*":
        break;
    case "-":
        break;
    default:

        break;

}
answerText.Text = Convert.ToString(sum);

I'm having problems modifying other variables outside of the switch statement - EX. rite, left;
Every time i try compiling the code ,it shows up with the error message "the name 'left' does not exist in the current context." and the same thing with the integer rite.

Upvotes: 0

Views: 552

Answers (9)

Bob Vale
Bob Vale

Reputation: 18474

rite and left are local to the inside of your try statement. You need to declare them outside of your try statement and then assign their values inside.

eg .

    string gr = comboBox1.ValueMember;
    decimal sum = 0M;
    decimal rite= 0M;
    decimal left= 0M;
    try
    {
        rite = Convert.ToDecimal(textBox1.Text);
        left = Convert.ToDecimal(textBox2.Text);
    }
    catch (Exception)
    {
        string swr = "Please enter REAL a number, can be with decimals";
        label2.Text = swr;
    }
    switch (gr)
    {
        case "X":

            sum = rite * left;
            break;
        case "/":

            break;
        case "*":
            break;
        case "-":
            break;
        default:

            break;

    }
    answerText.Text = Convert.ToString(sum);
}

Upvotes: 0

rite and left are only accessible within the scope where they are declared, i.e. inside the try block.

You need to do something like this

    decimal rite = 0m;
    decimal left = 0m;
    try
    {
        rite = Convert.ToDecimal(textBox1.Text);
        left = Convert.ToDecimal(textBox2.Text);
    }
    catch (Exception)
    {
        string swr = "Please enter REAL a number, can be with decimals";
        label2.Text = swr;
    }

although this is not really enough, since you must decide what values you want in rite and left if an exception is thrown.

Upvotes: 5

CharithJ
CharithJ

Reputation: 47530

Either include all the logic (switch) inside the try block or move the variable declaration outside of the try block.

Upvotes: 0

Mohammed Asaad
Mohammed Asaad

Reputation: 138

the two variables rite,left are declared as local just for try block and switch block can't access it,

you should declare them to be accessible by the two blocks

decimal rite=0m;
decimal left=0m;
        try
        {
            rite = Convert.ToDecimal(textBox1.Text);
            left = Convert.ToDecimal(textBox2.Text);
        }
        catch (Exception)
        {
            string swr = "Please enter REAL a number, can be with decimals";
            label2.Text = swr;
        }
        switch (gr)
        {
            case "X":

                sum = rite * left;
                break;
            case "/":

                break;
            case "*":
                break;
            case "-":
                break;
            default:

                break;

        }

Upvotes: 0

Drake
Drake

Reputation: 3891

You are declaring left and right in a try statement so they dont exist outside of the try statement.

        decimal rite;
        decimal left;

        try
        {
           rite = Convert.ToDecimal(textBox1.Text);
           left = Convert.ToDecimal(textBox2.Text);
        }
        catch (Exception)
        {
            string swr = "Please enter REAL a number, can be with decimals";
            label2.Text = swr;
        }

This is the correct answer. Its called variable scope.

Upvotes: 0

Dan Abramov
Dan Abramov

Reputation: 268255

They are declared in try block and therefore are scoped to it.
Also, what would you expect either of them to be when there is an exception in try block? They need to be initialized. (I assumed you're fine with zeroes as default values.)

Move them out:

    string gr = comboBox1.ValueMember;
    decimal left = 0M, right = 0M, sum = 0M; // assuming you want zeroes by default
    try
    {
        right = Convert.ToDecimal(textBox1.Text);
        left = Convert.ToDecimal(textBox2.Text);
    }
    catch (Exception)
    {
        string swr = "Please enter REAL a number, can be with decimals";
        label2.Text = swr;
    }
    switch (gr)
    {
        case "X":

            sum = right * left;
            break;
        case "/":

            break;
        case "*":
            break;
        case "-":
            break;
        default:

            break;

    }

P.S. Saving one character (rite instead of right) is not worth the effort, really.

Upvotes: 0

BBC
BBC

Reputation: 211

Put the declaration of "rite" and "left" along with gr and sum.

Upvotes: 0

David Wick
David Wick

Reputation: 7095

left and rite only exist inside the try code block.

Upvotes: 1

Ed Swangren
Ed Swangren

Reputation: 124652

Yes, because you have declared rite (I think you mean "right"...) and left inside the scope of a try/catch block. What happens if that first call to Convert.ToDecimal fails? Well, then rite has never been assigned and left has never even been declared. You need to bring them up a level and declare them outside the try/catch. Just do the assignment from within the try block.

Aside from that, you should be exiting the function if an error occurs as the code that comes later expects rite and left to be valid, which they won't be if that catch handler executes.

Upvotes: 0

Related Questions