Variable '<variablename>' hides a variable in an enclosing block

廉价感情. 提交于 2019-12-01 22:21:56

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!