How do I resolve this scope issue in VB .NET?

Posted by froadie on Stack Overflow See other posts from Stack Overflow or by froadie
Published on 2010-04-16T14:19:32Z Indexed on 2010/04/16 14:23 UTC
Read the original article Hit count: 216

Filed under:
|
|

I have a code structure something like this:

For row = 1 To numRows
   Dim arrayIndex As Integer = 0
   For column As Integer = bucketStartColumn To bucketEndColumn
      ' whatever code
      arrayIndex = arrayIndex + 1
   Next
Next

Dim arrayIndex As Integer = 0
For column As Integer = bucketStartColumn To bucketEndColumn
   ' whatever code
   arrayIndex = arrayIndex + 1
Next

Not exactly the code, so I don't really need suggestions about refactoring, but my problem is this - with this code I get a compiler error for the first Dim arrayIndex As Integer = 0 - "Variable 'arrayIndex' hides a variable in an enclosing block." As far as I can tell, arrayIndex is local to the first for loop and shouldn't exist by the time we reach the second loop. If I try to change the second declaration of arrayIndex to arrayIndex = 0, I get the error "Name 'arrayIndex' is not declared", as I expected. So is it visible, or not? Does this have something to do with the Dim keyword? Any suggestions of how to get around this, other than naming the second index variable something else?

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about variable-scope