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

发布于 2024-11-27 12:29:51 字数 845 浏览 0 评论 0原文

考虑以下 C# 代码:

string gr = comboBox1.ValueMember;
decimal sum;
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 = 12M;
        break;
    case "/":
        break;
    case "*":
        break;
    case "-":
        break;
    default:
        break;
}

answerText.Text = Convert.ToString(sum);

如果我在 switch 语句期间给十进制 sum 一个值,它将弹出一条错误语句,其中显示:

使用未分配的局部变量“sum”

我是 C# 的新手,所以这听起来可能很愚蠢。看起来我已经在 switch 语句中设置了 sum 的值。我尝试在所有其他 case 语句中放入相同的 sum = 12M; ,但这似乎没有帮助。

顺便说一句,我在修改 switch 语句之外的其他变量时也遇到问题 - EX。仪式,左;

Consider this C# code:

string gr = comboBox1.ValueMember;
decimal sum;
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 = 12M;
        break;
    case "/":
        break;
    case "*":
        break;
    case "-":
        break;
    default:
        break;
}

answerText.Text = Convert.ToString(sum);

If I give the decimal sum a value during the switch statement, it will popup with an error statement saying:

Use of unassigned local variable 'sum'

I'm a newbie at C# so this might sound stupid saying it. It looks like I ALREADY set the value of sum inside the switch statement. I tried putting the same sum = 12M; in all of the other case statements, but that doesn't seem to help.

By the way, im also having problems modifying other variables outside of the switch statement - EX. rite, left;

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

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

发布评论

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

评论(7

临走之时 2024-12-04 12:29:51

如果gr不等于“X”,sum没有值。编译器对此发出警告。

If gr is NOT equal to "X" sum has no value. The compiler is warning you for that.

烛影斜 2024-12-04 12:29:51

只有实例变量获得默认值,因此像sum这样的局部变量必须初始化,才能在其他地方使用。由于它可能不会被分配任何内容,因此编译器会引发错误。

Only instance variables get default values, so a local variable like sum, has to be initialized, for it to be used somewhere else. Since there is the possibility that it will not get assigned anything, the compiler raises an error.

给我一枪 2024-12-04 12:29:51

您只为一个条件设置 sum 的值,因此当您尝试将其转换为字符串时,它不会总是被分配。尝试将其声明为decimal sum = 0.0;

You are only setting the value of sum for ONE condition and hence it won't always be assigned at the point when you are trying to convert it to string. Try declaring it as decimal sum = 0.0;.

夏尔 2024-12-04 12:29:51

这是因为只有当 switch 语句遇到“X”情况时才会分配变量 sum。要解决此问题,请通过在顶部执行以下操作来设置默认值:

decimal sum = 0m;

This is because the variable sum is only assigned if the switch statement hits the "X" case. To fix this, set a default value by doing the following at the top:

decimal sum = 0m;
哭泣的笑容 2024-12-04 12:29:51

编译器检测到存在不会分配变量的执行路径。如果 grX 以外的任何内容,您将在 switch 语句后使用未分配的值。

您可以简单地将初始值添加到声明中:

decimal sum = 0m;

Compiler detected that a path of execution exists in which the variable won't be assigned. If gr is anything other than X you will be using an unassigned value after the switch statement.

You can simply add the initial value to the declaration:

decimal sum = 0m;
多彩岁月 2024-12-04 12:29:51

只需在声明时为其分配默认值,就不会出现错误:

decimal sum = 0;

Simply assign default value to it when declaring and you won't get error:

decimal sum = 0;
逆光飞翔i 2024-12-04 12:29:51

在声明时,您应该使用它:

decimal sum=0m;

编译器不确保第一种情况成立,因此仍可以使用 sum 而不分配

At declaration you should use this:

decimal sum=0m;

the compiler doesn't ensure that the first case will hold so sum may still be used without assigning

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