//...

 }

}

The AddContact() method takes in a Contact object and prints out the details of the contact. Suppose that the Contact class has a AddToAddressBook() method that takes in an AddressBook object. This method adds the Contact object into the AddressBook object:

public class Contact {

 public int ID;

 public string FirstName;

 public string LastName;

 public string Email;

 public void AddToAddressBook(AddressBook addBook) {

  addBook.AddContact(this);

 }

}

In this case, you use the this keyword to pass in the current instance of the Contact object into the AddressBook object. To test out that code, use the following statements:

Contact contact1 = new Contact();

contact1.ID = 12;

contact1.FirstName = 'Wei-Meng';

contact1.LastName = 'Lee';

contact1.Email = '[email protected]';

AddressBook addBook1 = new AddressBook();

contact1.AddToAddressBook(addBook1);

Properties

Properties are function members that provide an easy way to read or write the values of private data members. Recall the Contact class defined earlier:

public class Contact {

 public int ID;

 public string FirstName;

 public string LastName;

 public string Email;

}

You've seen that you can create a Contact object and set its public data members (ID, FirstName, LastName, and Email) directly, like this:

Contact c = new Contact();

c.ID = 1234;

c.FirstName = 'Wei-Meng';

c.LastName = 'Lee';

c.Email = '[email protected]';

However, if the ID of a person has a valid range of values — such as from 1 to 9999 — the following value of 12345 would still be assigned to the ID data member:

c.ID = 12345;

Technically, the assignment is valid, but logically it should not be allowed — the number assigned is beyond the range of values permitted for ID. Of course you can perform some checks before assigning a value to the ID member, but doing so violates the spirit of encapsulation in object- oriented programming — the checks should be done within the class.

A solution to this is to use properties.

The Contact class can be rewritten as follows with its data members converted to properties:

public class Contact {

 int _ID;

 string _FirstName, _LastName, _Email;

 public int ID {

  get {

   return _ID;

  }

  set {

   _ID = value;

  }

 }

 public string FirstName {

  get {

   return _FirstName;

  }

  set {

   _FirstName = value;

  }

 }

 public string LastName {

  get {

   return _LastName;

  }

  set {

   _LastName = value;

  }

 }

 public string Email {

  get {

   return _Email;

  }

  set {

   _Email = value;

  }

 }

}

Note that the public members (ID, FirstName, LastName, and Email) have been replaced by properties with the set and get accessors.

The set accessor sets the value of a property. Using this example, you can instantiate a Contact class and then set the value of the ID property, like this:

Contact c = new Contact();

c.ID = 1234;

In this case, the set accessor is invoked:

public int ID {

 get {

  return _ID;

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату