Creating Properties

Properties are much like fields as far as code outside your object is concerned , but internally, they use accessor methods to get and set their data, giving you the chance to add code that restricts what data is written to and read from a property.

For example, we have a class named Customer in which we want to support a property called Name , which holds the customer's name. To store that name, we'll use a private field called name :

 
 class Customer {  private string name;  .     .     . } 

FOR C++ PROGRAMMERS

Properties do not formally exist in C++.


To implement a property, you set up get and set accessor methods; the get method returns the property's value, and the set method sets it. Here's how to implement the get accessor method for this property, which simply returns the customer's name:

 
 class Customer {   private string name;  public string Name   {   get   {   return name;   }   .   .   .   }  } 

This example just returns the customer's name, but you can place whatever code you wanted here to process data as needed before returning that data (such as converting Fahrenheit temperatures into Centigrade), and that's what makes properties so powerful. In the set accessor method, you're passed the new value of the property in a parameter named value , which we'll store like this:

 
 class Customer {   private string name;   public string Name   {    get    {      return name;    }  set   {   name = value;   }  } } 

Now we can create objects of the Customer class, as well as set and get values using the Name property. You can see that at work in ch03_03.cs, Listing 3.3.

Listing 3.3 Creating a Property (ch03_03.cs)
 class ch03_03 {   static void Main()   {     Customer customer = new Customer();  customer.Name = "Nancy";   System.Console.WriteLine("The customer's name is {0}",   customer.Name);  } } class Customer {   private string name;   public string Name   {    get    {      return name;    }    set    {      name = value;    }   } } 

Here's what you see when you run ch03_03.cs. We have been able to set and retrieve a value with the new property:

 
 C:\>ch03_03 The customer's name is Nancy 

Property declarations take one of the following forms:

 
 [  attributes  ] [  modifiers  ]  type identifier  {  accessor-declaration  } [  attributes  ] [  modifiers  ]  type interface-type.identifier  {  accessor-declaration  } 

Here are the parts of this statement:

  • attributes (Optional) Hold additional declarative information, as we'll see in Chapter 14.

  • modifiers (Optional) The allowed modifiers are new , static , virtual , abstract , override , and a valid combination of the four access modifiers.

  • type The property type, which must be at least as accessible as the property itself.

  • identifier The property name.

  • accessor-declaration Declaration of the property accessors, which are used to read and write the property.

  • interface-type The interface in a fully qualified property name.

Note that unlike a class's fields, properties are not considered variables , which means it's not possible to pass a property as a ref or out parameter.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

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