Variable Scope Changes


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Appendix A.  VB6 Programming Element Changes in VB .NET

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 declared," in Visual Basic .NET on the line containing MsgBox I.


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222 © 2008-2017.
If you may any questions please contact us: