我声明了一个变量,但无法在 switch 语句内使用它

发布于 2024-11-27 09:44:41 字数 637 浏览 0 评论 0原文

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

我在修改 switch 语句之外的其他变量时遇到问题 - EX。仪式,左;
每次我尝试编译代码时,都会显示错误消息“当前上下文中不存在名称‘left’”。整数仪式也是如此。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

卷耳 2024-12-04 09:44:41

rite 和 left 只能在声明它们的范围内访问,即在 try 块内。

您需要做这样的事情,

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

尽管这还不够,因为您必须决定在仪式中想要什么值,并在引发异常时留下什么值。

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.

晌融 2024-12-04 09:44:41

leftrite 仅存在于 try 代码块内。

left and rite only exist inside the try code block.

浮生面具三千个 2024-12-04 09:44:41

是的,因为您已经在 try/catch 块的范围内声明了 rite (我认为您的意思是“右”...)和 left 。如果第一次调用 Convert.ToDecimal 失败会发生什么?那么,rite 从未被赋值,left 甚至从未被声明。您需要将它们提升一个级别并在 try/catch 之外声明它们。只需在 try 块内进行分配即可。

除此之外,如果发生错误,您应该退出该函数,因为后面的代码期望 riteleft 有效,但如果发生错误,它们就不会有效catch 处理程序执行。

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.

对不⑦ 2024-12-04 09:44:41

将“rite”和“left”的声明与gr 和sum 一起放置。

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

情深缘浅 2024-12-04 09:44:41

它们在 try 块中声明,因此其作用域为该块。
另外,当 try 块中出现异常时,您希望它们中的任何一个发生什么?
它们需要被初始化。 (我假设您可以使用零作为默认值。)

将它们移出:

    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;

    }

PS 保存一个字符(rite 而不是 right)确实不值得付出努力。

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.

一身仙ぐ女味 2024-12-04 09:44:41

您在 try 语句中声明了 left 和 right,因此它们不存在于 try 语句之外。

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

这是正确的答案。
它称为变量作用域。

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.

〗斷ホ乔殘χμё〖 2024-12-04 09:44:41

两个变量 rite,left 被声明为局部变量,仅用于 try 块,而 switch 块无法访问它,

您应该将它们声明为可由这两个块访问

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;

        }

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;

        }
扬花落满肩 2024-12-04 09:44:41

要么将所有逻辑(开关)包含在 try 块内,要么将变量声明移到 try 块之外。

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

紫瑟鸿黎 2024-12-04 09:44:41

rite 和 left 位于 try 语句内部。您需要在 try 语句之外声明它们,然后在其中分配它们的值。

例如。

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文