我声明了一个变量,但无法在 switch 语句内使用它
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
rite 和 left 只能在声明它们的范围内访问,即在 try 块内。
您需要做这样的事情,
尽管这还不够,因为您必须决定在仪式中想要什么值,并在引发异常时留下什么值。
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
although this is not really enough, since you must decide what values you want in rite and left if an exception is thrown.
left
和rite
仅存在于try
代码块内。left
andrite
only exist inside thetry
code block.是的,因为您已经在 try/catch 块的范围内声明了
rite
(我认为您的意思是“右”...)和left
。如果第一次调用Convert.ToDecimal
失败会发生什么?那么,rite
从未被赋值,left
甚至从未被声明。您需要将它们提升一个级别并在 try/catch 之外声明它们。只需在 try 块内进行分配即可。除此之外,如果发生错误,您应该退出该函数,因为后面的代码期望
rite
和left
有效,但如果发生错误,它们就不会有效catch 处理程序执行。Yes, because you have declared
rite
(I think you mean "right"...) andleft
inside the scope of a try/catch block. What happens if that first call toConvert.ToDecimal
fails? Well, thenrite
has never been assigned andleft
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
andleft
to be valid, which they won't be if that catch handler executes.将“rite”和“left”的声明与gr 和sum 一起放置。
Put the declaration of "rite" and "left" along with gr and sum.
它们在
try
块中声明,因此其作用域为该块。另外,当
try
块中出现异常时,您希望它们中的任何一个发生什么?它们需要被初始化。 (我假设您可以使用零作为默认值。)
将它们移出:
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:
P.S. Saving one character (
rite
instead ofright
) is not worth the effort, really.您在 try 语句中声明了 left 和 right,因此它们不存在于 try 语句之外。
这是正确的答案。
它称为变量作用域。
You are declaring left and right in a try statement so they dont exist outside of the try statement.
This is the correct answer.
Its called variable scope.
两个变量 rite,left 被声明为局部变量,仅用于 try 块,而 switch 块无法访问它,
您应该将它们声明为可由这两个块访问
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
要么将所有逻辑(开关)包含在 try 块内,要么将变量声明移到 try 块之外。
Either include all the logic (switch) inside the try block or move the variable declaration outside of the try block.
rite 和 left 位于 try 语句内部。您需要在 try 语句之外声明它们,然后在其中分配它们的值。
例如。
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 .