Access Control

for RuBoard

C# 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 who 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 everyone. Whenever we are implementing a class that anyone can use, we want to make it public .

Internal

The internal 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 7.) 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 internal 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 internal classes would not be accessible. So using public for class accessibility is generally a good idea.

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

Note that if you omit the access modifier in front of a class, internal will be the default used by the compiler.

Member Accessibility

Access to individual class members can be 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 internal 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.

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.

Internal

An internal member can be accessed from within classes in the same assembly but not from classes outside the assembly.

Internal Protected

An internal protected member can be accessed from within the assembly and from outside the assembly by a derived class.

Access Control in the Case Study

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

 public abstract class Reservable  {     static  private  int nextid = 0;  protected  int unitid;  internal protected  int capacity;  internal protected  decimal cost;  public  Reservable(int capacity, decimal cost)     {        this.capacity = capacity;        this.cost = cost;        unitid = nextid++;     }  } 

The static member nextid is strictly private , because it is used for autogenerating an id and has no use outside the class. The member 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 internal protected access-control specification is ideal for this case. Note that if we had used just internal , the program would have still compiled. But since later we may wish to implement derived classes in other assemblies, internal protected is more appropriate. Finally, the constructor is public .

for RuBoard


Application Development Using C# and .NET
Application Development Using C# and .NET
ISBN: 013093383X
EAN: 2147483647
Year: 2001
Pages: 158

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