Lesson 5: Scope and Access Levels

Lesson 5: Scope and Access Levels

Access levels define how types are instantiated and how members are accessed. You use access levels to encapsulate data and methods in your types, and to expose functionality to outside objects. In this lesson, you will learn how access modifiers control code access and how to use them in your types.

After this lesson, you will be able to

  • Explain the meanings of different access levels and how they affect access to classes, variables, and nested types

  • Explain what scope is and how it affects program execution

Estimated lesson time: 20 minutes

You can control how elements of your application are accessed by using access modifiers. Access modifiers are keywords such as Public (public), Private (private), and Friend (internal) that precede a variable or type declaration. The keyword that is used controls the level of access the member is allowed. When an access modifier precedes a member declaration, it affects the scope of that member, meaning it controls what code can access it. When a modifier precedes a type declaration, it determines both the scope of its members and how that type is instanced.

Member Access Modifiers

Type members can have modifiers to control their scope. Table 1.2 summarizes the different access levels.

Table 1-2. Access Levels

Access Modifier

Effect on Members

Public (Visual Basic .NET), public (Visual C#)

Can be accessed from anywhere.

Private (Visual Basic .NET), private (Visual C#)

Can be accessed only by members within the type that defines it.

Friend (Visual Basic .NET), internal (Visual C#)

Can be accessed from all types within the assembly, but not from outside the assembly.

Protected (Visual Basic .NET), protected (Visual C#)

Can be accessed only by members within the type that defines it or types that inherit from that type.

Protected Friend (Visual Basic .NET), protected internal (Visual C#)

Can be accessed from all types within the assembly or from types inheriting from the owning type. This is the union of Protected (protected) and Friend (internal) access.

Any member with the Public (public) modifier is visible to all code outside the class. Thus, other objects can access and modify public fields and can call public methods. Conversely, Private (private) methods are visible only inside the type to which they belong and cannot be accessed from the outside. A third access modifier, Friend (internal), indicates that members can be accessed by other types in the same assembly but cannot be accessed from types outside the assembly. The Protected (protected) modifier allows access from within the type to which the member belongs and to any types that inherit that type. The Protected Friend (protected internal) level provides the union of Protected (protected) and Friend (internal) access. For member variables, the access modifier can replace the Dim statement. If the Dim statement is used (in Visual Basic .NET) or no access modifier is used (in Visual C#), the variable is considered private in Visual C# and Visual Basic .NET classes, Public in Visual Basic .NET structures, and private in Visual C# structures. Methods do not require an access modifier. If no access modifier is specified, the method is Private (private) by default in a class or structure in C#, and Public (public) in a class or structure in Visual Basic .NET.

NOTE
Inheritance is discussed in depth in Chapter 4.

The following example demonstrates how to use the access modifiers and illustrates how they control access:

Visual Basic .NET

Public Class aClass ' This field can be accessed unconditionally by external ' code Public anInteger As Integer ' This method can be called by members of this class and ' assembly, but not by external code Friend Sub myMethod() End Sub ' This field can only be accessed by members of this class Private aString As String ' This method may be called by members of this class and any ' inheriting classes Protected Function Return1() As Integer Return 1 End Function ' This field may be accessed by members of the assembly or ' inheriting classes Protected Friend aLong As Long End Class

Visual C#

public class aClass { // This field can be accessed unconditionally by external // code public int anInteger; // This method can be called by members of this class and // assembly, but not by external code internal void myMethod() { } // This field can only be accessed by members of this class private string aString; // This method may be called by members of this class and // any inheriting classes protected int Return1() { return 1; } // This field may be accessed by members of the assembly or // inheriting classes protected internal long aLong; }

Type Access Modifiers

Structures and classes can also have access modifiers. Access modifiers control how a type can be instantiated and are similar to access modifiers for members. A Public (public) class can be instantiated by any object in the application. A Friend (internal) class can be created by other members of the assembly but cannot be created by objects external to the assembly. The Private (private) and Protected (protected) modifiers can be used only on nested types. A private class can be created only by objects of its own type or by types in which it is nested. Nested types also can be Protected (protected) or Protected Friend (protected internal), which allows classes inheriting the parent class to have access to them. Protected Friend (protected internal) classes are also visible to other members of the namespace. If no access modifier is specified for a class or a structure, it is considered Public (public).

NOTE
Protected members are discussed in greater detail in Chapter 4.

Access Modifiers for Nested Types

In general, a nested type is a type that is used exclusively by the type that contains it. Thus, it is usually a good practice to assign the Private (private) access modifier to a nested type. Under rare circumstances, you might want to create a nested type that can be created by other types and assign it a different access modifier. Although you can assign any access modifier to a nested type, the behavior will never be greater than the access modifier of the type that contains it. Consider the following example:

Visual Basic .NET

Friend Class ParentClass Public Class NestedClass End Class End Class

Visual C#

internal class ParentClass { public class NestedClass { } }

In this example, the nested class is declared Public (public) but is contained within a class that is marked Friend (internal). Although the nested class is public, it will not be visible to any classes outside the assembly by virtue of the parent class being marked Friend (internal). Thus, the nested class has a practical access level of Friend (internal).

Shared (static) Members

Regular members are unique to each object instance as shown in the following pseudocode:

Visual Basic .NET

Dim Object1 as New DemoClass() Dim Object2 as New DemoClass() Object1.MyField = 15 Object2.MyField = 20

Visual C#

DemoClass Object1 = new DemoClass(); DemoClass Object2 = new DemoClass(); Object1.MyField = 15; Object2.MyField = 20;

The MyField field holds a different value, depending on which instance of the class is referenced. It is also possible to have members that are common to all instances of a class. These members are called Shared (static) members. Only one instance of a Shared or static member can exist, no matter how many instances of a particular type have been created.

You can create a Shared (static) field by using the Shared (Visual Basic .NET) or static (Visual C#) keyword. For example:

Visual Basic .NET

Public Class Demo Public Shared MyField As Integer End Class

Visual C#

public class Demo { public static int MyField; }

Even though multiple instances of the Demo class might be instantiated, there will be only one copy of the MyField field. Note that the Shared (static) keyword is not an access modifier; rather, it specifies the member s shared nature. Shared members can still be Public (public), Private (private), Friend (internal), and so on.

Methods can be shared as well as fields. Whereas regular methods belong to instances of types, shared methods belong to the type itself. Because shared methods belong to the type itself, they cannot access instance data from any objects. They can only utilize shared variables, variables declared within the method, or parameters passed into the method.

Accessing Shared Members

Because Shared members belong to the type but not to object instances of a type, they should be accessed using the class name rather than the instance name. Although Visual Basic .NET allows you to access Shared members through the object, there is still only one instance of the Shared members. Visual C# is stricter in this regard and does not allow you to access static members through an object instance. An example is shown in the following code sample:

Visual Basic .NET

' This example uses the Demo class from the previous example Dim Object1 as New Demo() ' This is incorrect syntax. You should not access shared ' members through the object name, though it will not cause an ' error. Object1.MyField = 15 ' This syntax is correct-accessing the field through the class ' instead of the object. Demo.MyField = 15

Visual C#

// This example uses the Demo class from the previous example Demo Object1 = new Demo(); // This is incorrect syntax. You cannot access shared // members through the object name with Visual C# Object1.MyField = 15; // This syntax is correct-accessing the field through the class // instead of the object. Demo.MyField = 15;

Because Shared members belong to the type instead of any one instance of a type, it is not necessary to instantiate a type before accessing Shared members. Thus, you can call shared methods or retrieve shared fields before an instance of a type exists.

Lesson Summary

  • Access modifiers are used to control the scope of type members. There are five access modifiers: Public (public), Friend (internal), Private (private), Protected (protected), and Protected Friend (protected internal). Each provides varying levels of access.

  • If an access modifier is not specified for a method, it has a default access level of private in Visual C# classes and structures and public in Visual Basic .NET classes and structures. If an access modifier is not specified for a member variable, it has a default access level of private in a class or public in a structure.

  • Access modifiers also can be used on types to control how a type is instantiated. Access levels for types are as follows: Public (public) types can be instantiated from anywhere. Friend (internal) types can be instantiated only by members of the assembly, and Private (private) types can be instantiated only by themselves or within a containing type.

  • If no access modifier is specified for a class or a structure, it is considered Public (public).

  • Nested types obey the same rules as non-nested types, but in practice, they can never have an access level greater than that of their parent type.

  • Shared (static) members belong to the type but not to any instance of a type. They can be accessed without creating an instance of the type and are accessed using the type name instead of the instance name. Shared methods cannot refer to any instance data.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[.  .. ]0-316
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[. .. ]0-316
ISBN: 735619263
EAN: N/A
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net