Access Control

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 5.  Inheritance and Exceptions in VB.NET


VB.NET has two means for controlling accessibility of class members . Access can be controlled at both the class level and the member level.

Class Accessibility

An access modifier can be placed in front of the Class keyword to control which code can get at the class at all. Access can be further restricted by member accessibility, discussed in the next subsection.

Public

The most common access modifier of a class is Public , which makes the class available to all code in the program. Whenever we are implementing a class that all code can use, we want to make it Public .

Friend

The Friend modifier makes a class available within the current assembly, which can be thought of as a logical EXE or DLL. (Assemblies were introduced in Chapter 2 and will be discussed in more detail in Chapter 9.) All of our projects so far have built a single assembly, with both the client test program and the class(es) in this assembly. That means that if we had used Friend for the class modifier, the programs would have still worked. But later, if we put our classes into a DLL and tried to access them from a client program in a separate EXE, any Friend classes would not be accessible. So using Public for class accessibility is generally a good idea.

A common use of the Friend modifier is for helper classes that are intended to be used only within the current assembly, and not generally.

Member Accessibility

Access to individual class members is controlled by placing an access modifier such as Public or Private in front of the member. Member access can only further restrict access to a class, not widen it. Thus if you have a class with Friend accessibility, making a member Public will not make it accessible from outside the assembly.

Public

A Public member can be accessed from outside the class, even from other assemblies.

Private

A Private member can be accessed only from within the class (but not from derived classes).

Protected

Inheritance introduces a third kind of accessibility, Protected . A protected member can be accessed from within the class and from within any derived classes.

Friend

A Friend member can be accessed from within classes in the same assembly but not from classes outside the assembly.

Protected Friend

A Protected Friend member can be accessed by any class within the assembly and by any derived class from outside the assembly.

Access Control in the Case Study

The Reservable class in the file broker.vb illustrates the member access-control options that we have been discussing.

  Public  MustInherit Class Reservable  Private  Shared m_nextid As Integer = 0  Protected  m_unitid As Integer  Protected Friend  Capacity As Integer  Protected Friend  Cost As Decimal  Public  Sub New(_     ByVal capacity As Integer, _     ByVal cost As Decimal)       Me.Capacity = capacity       Me.Cost = cost       m_unitid = m_nextid       m_nextid += 1    End Sub End Class 

The shared member m_nextid is strictly Private , because it is used for automatically generating an ID and has no use outside the class. The member m_unitid is Protected because it is used in derived classes, such as Hotel , but not elsewhere. The members Capacity and Cost are used both in derived classes (such as Hotel ) and in the class Broker , which is not a derived class but is in the same assembly. The Protected Friend access-control specification is ideal for this case. Note that if we had used just Friend , the program would have still compiled. But since later we may wish to implement derived classes in other assemblies, Protected Friend is more appropriate. Finally, the constructor is Public , so that it can be accessed from any code in the program.


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

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