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

I'm interested in biological psychology and cognitive psychology
Tanya Reply
what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
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