我在 C# 中声明了一个变量,但无法在 switch 语句内使用它
考虑以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果
gr
不等于“X”,sum
没有值。编译器对此发出警告。If
gr
is NOT equal to "X"sum
has no value. The compiler is warning you for that.只有
实例变量
获得默认值,因此像sum
这样的局部变量必须初始化
,才能在其他地方使用。由于它可能不会被分配任何内容,因此编译器会引发错误。Only
instance variables
get default values, so a local variable likesum
, has to beinitialized
, for it to be used somewhere else. Since there is the possibility that it will not get assigned anything, the compiler raises an error.您只为一个条件设置 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;
.这是因为只有当 switch 语句遇到“X”情况时才会分配变量 sum。要解决此问题,请通过在顶部执行以下操作来设置默认值:
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:
编译器检测到存在不会分配变量的执行路径。如果
gr
是X
以外的任何内容,您将在 switch 语句后使用未分配的值。您可以简单地将初始值添加到声明中:
Compiler detected that a path of execution exists in which the variable won't be assigned. If
gr
is anything other thanX
you will be using an unassigned value after the switch statement.You can simply add the initial value to the declaration:
只需在声明时为其分配默认值,就不会出现错误:
Simply assign default value to it when declaring and you won't get error:
在声明时,您应该使用它:
编译器不确保第一种情况成立,因此仍可以使用 sum 而不分配
At declaration you should use this:
the compiler doesn't ensure that the first case will hold so sum may still be used without assigning