变量“<变量名>”将变量隐藏在封闭块中

发布于 2024-09-12 03:49:13 字数 762 浏览 7 评论 0原文

当从 MSDN 复制并粘贴一些示例代码时,我发现了标题中的错误 - 变量 '' 将变量隐藏在封闭块中

我复制的只是一个非常基本的 try 循环示例。

正如建议中所述“此错误的常见原因是在事件处理程序中使用 Catch e As Exception。如果是这种情况,请将 Catch 块变量命名为 ex 而不是 e。”

所以,我这样做了,将 e 更改为 ex 并且它有效,但是,我不明白为什么这不会导致相同的错误。

有人可以更好地解释一下错误是什么以及为什么 e 会导致该错误,而 ex 不会?

编辑-

代码示例

    Try
    Catch e As Exception
        msgbox(e.Message)
    End Try

...

    Try
    Catch ex As Exception
        msgbox(ex.Message)
    End Try

我不明白的是为什么第一个会导致问题而第二个不会,对我来说,就像......使用上面的苹果,下面的苹果 - 说你不能在两个地方使用相同的东西,然后将两者都改为橙色并突然让它发挥作用。当然,第二个的做法与第一个相同。

When copying and pasting a bit of sample code from MSDN, I came up with the error in the title - Variable '' hides a variable in an enclosing block,

All I copied was a very basic example of a try loop.

As it says in the suggestion "A common cause for this error is the use of Catch e As Exception inside an event handler. If this is the case, name the Catch block variable ex rather than e."

So, I did that, changed both e to ex and it worked, however, I don't understand why this doesn't cause the same error.

Can someone please explain better what the error is and why e causes it, and ex doesn't?

edit -

code example...

    Try
    Catch e As Exception
        msgbox(e.Message)
    End Try

.

    Try
    Catch ex As Exception
        msgbox(ex.Message)
    End Try

What I don't understand is why the first one causes the problem and the second one doesn't, to me, it is like... Using apples above, apple below - saying you can't use the same thing in both places, then changing both to oranges and suddenly letting it work. Surely the second is doing the same as the first.

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

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

发布评论

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

评论(3

殊姿 2024-09-19 03:49:14

您可能想要粘贴错误的完整代码以进行确认,但我假设事件处理程序定义了一个名为“e”的参数。然后,当您放入 catch 块时,它也会尝试定义“e”,从而导致出现相关错误。当然,当问题定义“ex”而不是“e”时,就不会发生名称冲突,因此它可以工作。

编辑:编辑以添加更清晰的示例,说明我认为损坏的代码。

我假设您的破坏代码如下所示:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
    Catch e As Exception
        msgbox(e.Message)
    End Try
End Sub

您可以看到 e 的两个声明,一个在 ByVal e As System.EventArgs 中,另一个在 Catch e As Exception 中。

You might want to paste the full code for the error to confirm but I would assume that the event handler defines a parameter called "e". Then when you put in the catch block it tries to define "e" as well, causing the error in question. Of course when the catch is defining "ex" instead of "e" then there is no name clash happening so it works.

Edit: Edited to add clearer example of what I assume is the breoken code.

I assume your breaking code looks like:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
    Catch e As Exception
        msgbox(e.Message)
    End Try
End Sub

You can see the two declarations of e, one in ByVal e As System.EventArgs and the other at Catch e As Exception.

抱着落日 2024-09-19 03:49:14

该错误消息意味着您正在声明一个名称已存在的变量:

int abc = 0;
if (abc == 0)  {
  int abc = 1;  // Error
}

此规则当然也适用于 try .. catch 。

That error message means that you are declaring a variable with a name that already exists:

int abc = 0;
if (abc == 0)  {
  int abc = 1;  // Error
}

This rule applies of course to try .. catch as well.

北座城市 2024-09-19 03:49:14

是的。将导致问题的变量重命名为唯一的名称。

Yes. rename the variable causing the problem to a unique name.

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