变'< VARIABLENAME>“在封闭块中隐藏变量变量、LT、GT、VARIABLENAME

2023-09-03 05:51:54 作者:我只是找个借口留下来〃

在复制和粘贴一点从MSDN样本code,我想出了在标题中错误 - 的变'隐藏在封闭块的一个变量,

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.

,因为它说的建议的一个常见原因这个错误是使用捕捉电子商务作为异常事件处理中,如果是这样的话,命名Catch块可变前,而不是如

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.

有人可以解释更好的错误是什么,为什么E,其导致的,和前不?

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

编辑 -

code例如...

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.

推荐答案

您可能要贴全code的错误进行确认,但我会假设该事件处理程序定义一个名为E的参数。然后,当你把catch块它试图定义E为好,导致有问题的错误。当然,当渔获定义,而不是E恩那么就没有名字冲突发生这样它的工作原理。

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.

编辑:编辑补充什么,我认为是breoken code更清晰的例子

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

我假设你的破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

您可以看到E,一两个声明中 BYVALË作为System.EventArgs 和其他在卡子E作为例外

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