Variable Scope Changes

Block scope has been added to Visual Basic .NET. If you declared a variable in a block like a loop construct in VB6, that variable was also visible in the outer, containing scope. The following code fragment from VB6 demonstrates.

Private Sub Command3_Click()
  If True Then
    Dim I As Integer
    I = 5
  End If
  MsgBox I
End Sub

The variable I is declared inside the If..Then block terminated by the End If clause. In Visual Basic 6, the preceding code was valid and the message box would display the value of I, which is 5.

Visual Basic .NET supports block level scope. This means that the variable I in the example fragment is not visible outside the If Then..End If block. The code fragment would incur an error, “The name 'I' is not ...

Get Visual Basic® .NET Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.