<< Chapter < Page
  Xna game studio     Page 9 / 12
Chapter >> Page >

Call the object's public accessor methods

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 setColor method

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 getColor 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).

Would be a property in Java

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.

The definition of setColor and getColor in TargetClass

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

Typical method definitions

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.

Data validation

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.

Questions & Answers

what is chemistry
ISIYAKA Reply
what is oxidation
Chidiebube Reply
calculate molarity of NaOH solution when 25.0ml of NaOH titrated with 27.2ml of 0.2m H2SO4
Gasin Reply
what's Thermochemistry
rhoda Reply
the study of the heat energy which is associated with chemical reactions
Kaddija
How was CH4 and o2 was able to produce (Co2)and (H2o
Edafe Reply
explain please
Victory
First twenty elements with their valences
Martine Reply
what is chemistry
asue Reply
what is atom
asue
what is the best way to define periodic table for jamb
Damilola Reply
what is the change of matter from one state to another
Elijah Reply
what is isolation of organic compounds
IKyernum Reply
what is atomic radius
ThankGod Reply
Read Chapter 6, section 5
Dr
Read Chapter 6, section 5
Kareem
Atomic radius is the radius of the atom and is also called the orbital radius
Kareem
atomic radius is the distance between the nucleus of an atom and its valence shell
Amos
Read Chapter 6, section 5
paulino
Bohr's model of the theory atom
Ayom Reply
is there a question?
Dr
when a gas is compressed why it becomes hot?
ATOMIC
It has no oxygen then
Goldyei
read the chapter on thermochemistry...the sections on "PV" work and the First Law of Thermodynamics should help..
Dr
Which element react with water
Mukthar Reply
Mgo
Ibeh
an increase in the pressure of a gas results in the decrease of its
Valentina Reply
definition of the periodic table
Cosmos Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Xna game studio. OpenStax CNX. Feb 28, 2014 Download for free at https://legacy.cnx.org/content/col11634/1.6
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Xna game studio' conversation and receive update notifications?

Ask