Access Modifiers Assignment The Visual Basic .NET Language

44

2.9 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 doesnt 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 dont count on this behavior, as it is not guaranteed and is the kind of thing that might change in future versions of Visual Basic.

2.10 Access Modifiers

Access modifiers control the accessibility of types including enumerations, structures, classes, standard modules, and delegates and type members including methods, constructors, events, constants, fields [data members], and properties to other program elements. They are part of the declarations of types and type members. In the following code fragment, for example, the keywords Public and Private are access modifiers: Public Class SomeClass Public Sub DoSomething ... End Sub Private Sub InternalHelperSub ... End Sub End Class The complete list of access modifiers and their meanings is shown in Table 2- 5 . 45 Table 2-5. Access modifiers Access modifier Description Friend Defines a type that is accessible only from within the program in which it is declared. Private Defines a type that is accessible only from within the context in which it is declared. For instance, a Private variable declared within a class module is accessible only from within that class module. A Private class is accessible only from classes within which it is nested. Protected Applies to class members only. Defines a type that is accessible only from within its own class or from a derived class. Protected Friend Defines a type that is accessible from within the program in which it is declared as well as from derived classes. Public Defines a type that is publicly accessible. For example, a public method of a class can be accessed from any program that instantiates that class.

2.11 Assignment

In Visual Basic .NET, assignment statements are of the form: variable, field, or property = expression Either the type of the expression must be the same as that of the item receiving the assignment, or there must exist an appropriate implicit or explicit conversion from the type of the expression to the type of the item receiving the assignment. For information on implicit and explicit conversions, see Sect ion 2.5.5 earlier in this chapter. When an assignment is made to a value type, the value of the expression is copied to the target. In contrast, when an assignment is made to a reference type, a reference to the value is stored in the target. This is an important distinction that is worth understanding well. Consider the code in Ex am ple 2- 3 . Example 2-3. Value-type assignment versus reference-type assignment Public Structure SomeStructure Public MyPublicMember As String End Structure Public Class SomeClass Public MyPublicMember As String End Class Public Class AssignmentTest Public Shared Sub TestValueAndReferenceAssignment Dim a, b As SomeStructure Dim c, d As SomeClass Test assignment to value type. a.MyPublicMember = To be copied to b b = a a.MyPublicMember = New value for a Console.WriteLineThe value of b.MyPublicMember is _ 46 b.MyPublicMember Test assignment to reference type. c = New SomeClass c.MyPublicMember = To be copied to d d = c c.MyPublicMember = New value for c Console.WriteLineThe value of d.MyPublicMember is _ d.MyPublicMember End Sub End Class The output of the TestValueAndReferenceAssignment method in Ex am ple 2- 3 is: The value of b.MyPublicMember is To be copied to b The value of d.MyPublicMember is New value for c In Ex am ple 2- 3 , the SomeStructure structure and the SomeClass class have identical definitions, except that one is a structure and the other is a class. This leads to very different behavior during assignment. When a value type is copied, the actual value is copied. When a reference type is copied, only the reference is copied, resulting in two references to the same value. If the value is subsequently changed through one of the references, the new value is also seen through the other reference. This difference is shown in the output from Ex am ple 2- 3 . The value type in variable a is copied to variable b . The value of a.MyPublicMember is then modified. Subsequently, the call to Console.WriteLine shows that this modification does not affect b.MyPublicMember . In contrast, the assignment of c to d copies only a reference, which means that after the assignment, both c and d reference the same object. The value of c.MyPublicMember is then modified. The subsequent call to Console.WriteLine shows that this modification did affect d.MyPublicMember . Indeed, d.MyPublicMember refers to the same memory as c.MyPublicMember .

2.12 Operators and Expressions