How do I resolve this scope issue in VB .NET?
- by froadie
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?