Skip to Content
Programming Visual Basic .NET
book

Programming Visual Basic .NET

by Dave Grundgeiger
December 2001
Beginner
464 pages
13h 51m
English
O'Reilly Media, Inc.
Content preview from Programming Visual Basic .NET

Scope

Scope refers to the so-called visibility of identifiers within source code. That is, given a particular identifier declaration, the scope of the identifier determines where it is legal to reference that identifier in code. For example, these two functions each declare a variable CoffeeBreaks. Each declaration is invisible to the code in the other method. The scope of each variable is the method in which it is declared.

Public Sub MyFirstMethod(  )
   Dim CoffeeBreaks As Integer
   ' ...
End Sub
 
Public Sub MySecondMethod(  )
   Dim CoffeeBreaks As Long
   ' ...
End Sub

Unlike previous versions of Visual Basic, Visual Basic .NET has block scope . Variables declared within a set of statements ending with End, Loop, or Next are local to that block. For example:

Dim i As Integer
For i = 1 To 100
   Dim j As Integer
   For j = 1 To 100
      ' ...
   Next
Next
' j is not visible here

Visual Basic .NET doesn’t permit the same variable name to be declared at both the method level and the block level. Further, the life of the block-level variable is equal to the life of the method. This means that if the block is re-entered, the variable may contain an old value (don’t count on this behavior, as it is not guaranteed and is the kind of thing that might change in future versions of Visual Basic).

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Visual Basic .NET, Second Edition

Programming Visual Basic .NET, Second Edition

Jesse Liberty

Publisher Resources

ISBN: 0596000936Supplemental ContentCatalog PageErrata