Windows Phone Developers

Tuesday, March 30, 2010

Accessors and mutators in C#

Accessors and mutators do the job of retrieving and storing the data to a class member. The class member will be usually private and hence cannot be accessed directly by the object.

The following code provides a hint of the same



public enum playerType { ClassA, ClassB, ClassC, Contract }
private playerType _PlayerType;
string Name;
string Age;
string Phone;


#endregion

#region properties
public playerType PlayerType
{
//accessor
get { return _PlayerType; }

//mutator
set
{
_PlayerType = value;
}

}


Here _PlayerType is declared as private. This can be however accessed in the main program as shown below:


ClassTeam CT = new ClassTeam();
CT.PlayerType = ClassTeam.playerType.Contract; //Calls the Mutator
MessageBox.Show(CT.PlayerType.ToString()); // Calls the Accessor


The statement CT.PlayerType = ClassTeam.playerType.Contract; Calls the Mutator (set property) and sets the value and CT.PlayerType.ToString() uses Get property and retrieves the value Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment