<< 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 phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
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