Handling Methods and Functions - Encapsulating Data with Properties
(Page 3 of 5 )
Properties allow clients to access class state as though 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 and doesn’t want to work with methods. The class designer, however, wants to hide the internal state of his class in class members, and provide indirect access through a method.
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 theTimeclass is first created, theHourvalue might be stored as a member variable. When the class is redesigned, theHour value might be computed or retrieved from a database. If the client had direct access to the originalHourmember variable, the change to computing the value would break the client. By decoupling and forcing the client to go through a method (or property), theTimeclass can change how it manages its internal state without breaking client code.
Properties meet both goals: they provide a simple interface to the client, appearing to be a member variable. They are implemented as methods, however, providing the data-hiding required by good object-oriented design, as illustrated in Example 4-11.
Example 4-11. Using a property
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace UsingAProperty
{
public class Time
{
// private member variables
private int year;
private int month;
private int date;
private int hour;
private int minute;
private int second;
// public accessor methods
public void DisplayCurrentTime()
{
System.Console.WriteLine(
"Time\t: {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;
}
// create a property
public int Hour
{
get
{
return hour;
}
set
{
hour = value;
}
}
}
public class Tester
{
static void Main()
{
System.DateTime currentTime = System.DateTime.Now;
Time t = new Time( currentTime );
t.DisplayCurrentTime();
int theHour = t.Hour;
System.Console.WriteLine( "\nRetrieved the hour: {0}\n",
theHour );
theHour++;
t.Hour = theHour;
System.Console.WriteLine( "Updated the hour: {0}\n", theHour );
}
}
}
To declare a property, write the property type and name followed by a pair of braces. Within the braces you may declaregetandsetaccessors. Neither of these has explicit parameters, though theset(), accessor has an implicit parametervalue, as shown next.
In Example 4-11,Houris a property. Its declaration creates two accessors:getandset:
public int Hour
{
get
{
return hour;
}
set
{
hour = value;
}
}
Each accessor has an accessor body that does the work of retrieving and setting the property value. The property value might be stored in a database (in which case the accessor body would do whatever work is needed to interact with the database), or it might just be stored in a private member variable:
private int hour;
Next: The get Accessor >>
More C# Articles
More By O'Reilly Media