<< Chapter < Page | Chapter >> Page > |
Returning to the Main method in the class named Props01 , Listing 3 calls the object's public accessor methods named setColor and getColor twice in succession.
Listing 3 . Call the object's public accessor methods.
//Call public accessor methods
obj.setColor("Red");Console.WriteLine("color: " + obj.getColor());
//Call public accessor methods again with an// invalid input value.
obj.setColor("Green");Console.WriteLine("color: " + obj.getColor());
The purpose of the method named setColor is to store a string value in the object. The calling code has no way of knowing how the valueis stored because the implementation is hidden behind a public interface method. This is an example of encapsulation.
The string value to be stored is passed as a parameter to the method each time the method is called. An invalid string value was purposely passed as aparameter on the second call to the setColor method.
The purpose of the getColor method is to retrieve the string value previously stored in the object by the setColor method. Once again, the calling code has no way of knowing how the value is retrievedand returned because the implementation is hidden behind a public interface method.
The value that is retrieved by each call to the getColor method is displayed on the standard output device (the black screen).
If this were a Java program, the combination of these two methods would constitute a property named color because the pair of methods matches the design pattern for properties in Java. However, that is not the casein C#. As you will see later, C# uses a different approach to identify properties. In C#, these are simply public accessor methods used for informationhiding.
Listing 4 defines the public setColor and getColor methods in the class named TargetClass .
Listing 4 . Definition of setColor and getColor methods in TargetClass.
private string colorData = "";
public void setColor(string data){//Validating code
if(data.Equals("Red") || data.Equals("Blue")){//Accept incoming data value
colorData = data;}else{
//Reject incoming data valuecolorData = "Bad color";
}//end if-else}//end setColor
//--------------------------------------------------//public string getColor(){
return colorData;}//end getColor
These two methods are typical of the method definition syntax in C#. The syntax is not too different from a function definition in C++ so you should haveno trouble understanding the syntax.
One of the reasons for hiding the implementation behind public accessor methods is to assure that only valid data isstored in the object. Therefore, public accessor methods that store data in the object often contain code that validates the incoming data (as shown in Listing 4 ) before actually storing the data in the object.
The validation code in Listing 4 will only accept incoming color data of "Red" or "Blue". If the incoming string doesn't match one of those two values,the string "Bad color" is stored in the object in place of the incoming value.
Notification Switch
Would you like to follow the 'Xna game studio' conversation and receive update notifications?