.NET – Variable “hides variables in a closed block

When copying and pasting some sample code from MSDN, I came up with the error in the title – Variable ”hides a variable in an enclosing block,

What I copied is just a very basic example of a try loop.

As it says in the suggestion, “A common cause of this error is the use of Catch e As Exception in the event handler. If it is In this case, please name the Catch block variable as ex instead of e.”

So, I did this, changed e to ex and it worked, but, I don’t understand why this does not cause the same The error.

Can someone better explain what the error is and why it causes it, while the former is not?

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 caused Problem, and the second one doesn’t cause problems, to me, it’s like…use the apple above, the apple below – say you can’t use the same thing in two places and then change both to oranges and suddenly let It works. Of course the second one is the same as the first one.

you may want to paste the wrong Complete code for confirmation, but I think the event handler defines a parameter named “e”. Then, when you put in the catch block, it will also try to define “e”, which leads to an error. Of course, when catch When defining “ex” instead of “e”, no name conflict occurs, so it works.

Edit: Edited to add a clearer example, I assume it is the breathen code.

p>

I assume that your cracking code is as follows:

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

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

When copying and pasting some sample code from MSDN, I came up with the error in the title – Variable ”hides a variable in an enclosing block,

What I copied is just a very basic example of a try loop.

As It says in the suggestion “A common cause of this error is the use of Catch e As Exception in the event handler. If this is the case, please name the Catch block variable ex instead of e.”

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

Can someone better explain what the error is and why Cause it, and the former is not?

Edit –

Code example…

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

.

Try
Catch ex As Exception
msgbox(ex.Message)< br /> End Try

What I don’t understand is why the first one causes the problem, while the second one does not cause the problem. For me, it’s like…using the apple above, the apple below – Say you can’t use the same thing in two places and then change both to oranges and suddenly let it work. Of course the second one is the same as the first one.

You may want to paste the wrong complete code for confirmation, but I think the event handler defines a parameter called “e”. Then, when you put in the catch block, it will also try to define “e “, resulting in an error. Of course, when catch defines “ex” instead of “e”, no name conflict occurs, so it works.

Edit: Edit to add clearer For example, I assume it is the breoken code.

I assume that your cracking code is as follows:

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 TryEnd Sub

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

Leave a Comment

Your email address will not be published.