开发者

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

开发者 https://www.devze.com 2023-03-24 04:57 出处:网络
string gr = comboBox1.ValueMember; decimal sum = 0M; try { decimal rite = Convert.ToDecimal(textBox1.Text);
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.


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.


left and rite only exist inside the try code block.


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.


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


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.


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.


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;

        }


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


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号