DekGenius.com
Team LiB   Previous Section   Next Section

9.2 Encapsulating Data with Properties

It is generally desirable to designate the member variables of a class as private. This means that only member methods of that class can access their value. You make member variables private to support data hiding, which is part of the encapsulation of a class.

Object-oriented programmers are told that member variables should be private. That is fine, but how do you provide access to this data to your clients? The answer for C# programmers is properties. Properties allow clients to access class state as if they were accessing member fields directly, while actually implementing that access through a class method.

This is ideal. The client wants direct access to the state of the object. The class designer, however, wants to hide the internal state of the class in class fields and provide indirect access through a method. The property provides both: the illusion of direct access for the client and the reality of indirect access for the class developer.

By decoupling the class state from the method that accesses that state, the designer is free to change the internal state of the object as needed. When the Time class is first created, the Hour value might be stored as a member variable. When the class is redesigned, the Hour value might be computed or retrieved from a database. If the client had direct access to the original Hour member variable, the change to computing the value would break the client. By decoupling and forcing the client to go through a property, the Time class can change how it manages its internal state without breaking client code.

In short, properties provide the data hiding required by good object-oriented design. Example 9-2 creates a property called Hour, which is then discussed in the paragraphs that follow.

Example 9-2. Properties
using System;

namespace Properties
{
    public class Time
    {

        // private member variables
        private int year;
        private int month;
        private int date;
        private int hour;
        private int minute;
        private int second;
 
        // create a property
        public int Hour 
        { 
            get 
            { 
                return hour; 
            } 

            set 
            { 
                hour = value; 
            } 
        } 

        // public accessor methods
        public void DisplayCurrentTime()
        {
            System.Console.WriteLine(
                "Time: {0}/{1}/{2} {3}:{4}:{5}",
                month, date, year, hour, minute, second);
        }


        // constructors
        public Time(System.DateTime dt)
        {
            year =      dt.Year;
            month =     dt.Month;
            date =      dt.Day;
            hour =      dt.Hour;
            minute =    dt.Minute;
            second =    dt.Second;
        }


   }
   class Tester
   {
      public void Run()
      {
          System.DateTime currentTime = System.DateTime.Now;
          Time t = new Time(currentTime);
          t.DisplayCurrentTime();

          // access the hour to a local variable
          int theHour = t.Hour; 

          // display it
          System.Console.WriteLine("Retrieved the hour: {0}",
              theHour);

          // increment it
          theHour++;

          // reassign the incremented value back through
          // the property
          t.Hour = theHour; 

          // display the property
          System.Console.WriteLine("Updated the hour: {0}", t.Hour);
      }

      [STAThread]
      static void Main()
      {
         Tester t = new Tester();
         t.Run();
      }
   }
}
 Output:
Time    : 3/26/2002 12:7:43
Retrieved the hour: 12
Updated the hour: 13

You create a property by writing the property type and name followed by a pair of braces. Within the braces you can declare the get and set accessors. These accessors are very similar to methods, but they are actually part of the property itself.

Neither of these accessors has explicit parameters, though the set accessor has an implicit parameter value.

By convention, property names are written in Pascal notation (initial uppercase).

In Example 9-2, the declaration of the Hour property creates both get and set accessors:

public int Hour
{
    get
    {
        return hour;
    }

    set
    {
        hour = value;
    }
}

Each accessor has an accessor-body, which does the work of retrieving and setting the property value. The property value might be stored in a database (in which case the accessor would do whatever work is needed to interact with the database), or it might just be stored in a private member variable (in this case, hour):

private int hour;

9.2.1 The get Accessor

The body of the get accessor is similar to a class method that returns an object of the type of the property. In Example 9-2, the accessor for the Hour property is similar to a method that returns an int. It returns the value of the private member variable hour in which the value of the property has been stored:

get
{
    return hour;
}

In this example, the value of a private int member variable is returned, but you could just as easily retrieve an integer value from a database or compute it on the fly.

Whenever you reference the property (other than to assign to it), the get accessor is invoked to read the value of the property. For example, in the following code the value of the Time object's Hour property is assigned to a local variable. The get accessor is called, which returns the value of the Hour member variable, and that value is assigned to the local variable named theHour.

Time t = new Time(currentTime);
int theHour = t.Hour;

9.2.2 The set Accessor

The set accessor sets the value of a property. When you define a set accessor you must use the value keyword to represent the argument whose value is passed to and stored by the property.

set
{
     hour = value;
}

Here, again, a private member variable is used to store the value of the property, but the set accessor could write to a database or update other member variables as needed.

When you assign a value to the property, the set accessor is automatically invoked, and the implicit parameter value is set to the value you assign:

theHour++;
t.Hour = theHour;

The advantage of this approach is that the client can interact with the properties directly, without sacrificing the data hiding and encapsulation sacrosanct in good object-oriented design.

You can create a read-only property by not implementing the set part of the property. Similarly, you can create a write-only property by not implementing the get part.

    Team LiB   Previous Section   Next Section