2.7 Constructing Class Properties

 <  Day Day Up  >  

You want to allow access to a member variable from client code without allowing direct access.


Technique

Right-click on the class you want to add the property to in the Class View window, and select Add, Add Property. Select the correct access modifier and data type and enter a property name in the corresponding fields in the Add Property dialog. If you want read-only access to the property, select the get accessor radio button while the set accessor allows write access. Choose one or both as appropriate. You might also make the property static or virtual by clicking the available property modifiers.

Comments

Properties are designed to properly enforce data hiding through encapsulation. If you were to just give public access to a member variable, the stability of your object could be compromised because you don't have control over the values placed in that variable. By using a class property, it appears that you are giving transparent access to a class member variable when in actuality you are routing the request to a member accessor method. Within this method, you can validate the value being assigned to a private member variable.

There can be two methods associated with any given property. If you are providing a read-only property accessor, then you are using a get method. Likewise, if you are allowing the variable to change based on the value being passed in, you are using a set method. You are free to do anything within these methods, which traditionally is to validate the data being passed in and to set or return the value of a private member variable.

The get method, as shown in Listing 2.6, simply returns the value of a private member variable. Because this method requires no data validation, the method body is simply a return statement.

The set method contains an implicit parameter that corresponds to the value of the code accessing the property used in the assignment statement. This parameter is named value . In Listing 2.6, you can see the set method for the Television class first validates that the value passed in isn't larger than the number of available channels and finally sets the internal channel member variable to the implicit parameter value.

Listing 2.6 Defining Properties to Provide Validating Member Variable Access Using Data-Hiding Techniques
 using System; namespace _6_Properties {     public class Television     {         private int channel = 1;         // this tv only has 255 channels         private const int maxChannels = 255;         // changes the channel on the tv         public int Channel         {             get             {                 return channel;             }             set             {                 // check value's range                 if( value > maxChannels )                     return;                 // set private variable                 channel = value;             }         }     } } 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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