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

What do you do with invalid data

It usually isn't too difficult to write code to implement a set of rules to validate the incoming data. The hard part is figuring out what to do if theincoming data is not valid. The choices range all the way from flagging the data as invalid as shown in Listing 4 to throwing an exception which, if not properly handled, will cause the program to terminate. The circumstances dictate theaction to be taken.

Program output

The code in Listing 3 calls the setColor method twice. The first call passes a valid string, "Red", as a parameter. The second call passes an invalid string, "Green", as a parameter. This causes the second and third lines of text in Figure 1 to be displayed by the program.

Set, get, and display a property value with a valid value

As mentioned earlier, C# uses a special approach to identify properties. (You will see the code in TargetClass that accomplishes this shortly.) In the meantime, the code in Listing 5 (which is still part of the Main method) first sets, then gets, and finally displays the value of a property named height belonging to the object referenced by the contents of the variable named obj .

Listing 5 . Set, get, and display a property value with a valid value.

obj.height = 20; Console.WriteLine("height: " + obj.height);

Looks can be deceiving

If you compare Listing 5 with Listing 1 , you will see that there is essentially no difference in the syntax of the code in the two listings. The syntax in both listings suggests that a value is beingdirectly assigned to a public instance variable belonging to the object. As we already know, that is true for Listing 1 . However, that is not true for Listing 5 .

A set Accessor method

Although it doesn't look like it, the code in Listing 5 is actually calling a special set Accessor method that hides the implementation behind a public interface. As you will see shortly, this special method contains validation code similarwhat you saw in Listing 4 .

A get Accessor method

Once again, although it doesn't look like it, the last statement in Listing 5 is actually calling a special get Accessor method belonging to the object to get the current value of the property. That method returns the value stored in the property. As before, thereturned value is concatenated with a literal string and passed to the WriteLine method of the Console class for display on the black screen.

The set Accessor and get Accessor methods for the property named height

Returning to the class named TargetClass , the special set Accessor and get Accessor methods for the property named height are shown in Listing 6 .

Listing 6 . The set Accessor and get Accessor methods for the property named height.

private int heightData; public int height{get{ return heightData;}//end get set{//Validating code if(value<84){ heightData = value;}else{ heightData = 0;}//end else }//end set}//end height property

A hidden parameter named value

The value on the right side of the assignment operator in Listing 5 arrives on the set side of the code in Listing 6 as a hidden parameter named value .

Questions & Answers

how do you get the 2/50
Abba Reply
number of sport play by 50 student construct discrete data
Aminu Reply
width of the frangebany leaves on how to write a introduction
Theresa Reply
Solve the mean of variance
Veronica Reply
Step 1: Find the mean. To find the mean, add up all the scores, then divide them by the number of scores. ... Step 2: Find each score's deviation from the mean. ... Step 3: Square each deviation from the mean. ... Step 4: Find the sum of squares. ... Step 5: Divide the sum of squares by n – 1 or N.
kenneth
what is error
Yakuba Reply
Is mistake done to something
Vutshila
Hy
anas
hy
What is the life teble
anas
hy
Jibrin
statistics is the analyzing of data
Tajudeen Reply
what is statics?
Zelalem Reply
how do you calculate mean
Gloria Reply
diveving the sum if all values
Shaynaynay
let A1,A2 and A3 events be independent,show that (A1)^c, (A2)^c and (A3)^c are independent?
Fisaye Reply
what is statistics
Akhisani Reply
data collected all over the world
Shaynaynay
construct a less than and more than table
Imad Reply
The sample of 16 students is taken. The average age in the sample was 22 years with astandard deviation of 6 years. Construct a 95% confidence interval for the age of the population.
Aschalew Reply
Bhartdarshan' is an internet-based travel agency wherein customer can see videos of the cities they plant to visit. The number of hits daily is a normally distributed random variable with a mean of 10,000 and a standard deviation of 2,400 a. what is the probability of getting more than 12,000 hits? b. what is the probability of getting fewer than 9,000 hits?
Akshay Reply
Bhartdarshan'is an internet-based travel agency wherein customer can see videos of the cities they plan to visit. The number of hits daily is a normally distributed random variable with a mean of 10,000 and a standard deviation of 2,400. a. What is the probability of getting more than 12,000 hits
Akshay
1
Bright
Sorry i want to learn more about this question
Bright
Someone help
Bright
a= 0.20233 b=0.3384
Sufiyan
a
Shaynaynay
How do I interpret level of significance?
Mohd Reply
It depends on your business problem or in Machine Learning you could use ROC- AUC cruve to decide the threshold value
Shivam
how skewness and kurtosis are used in statistics
Owen Reply
yes what is it
Taneeya
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