Validation for menu options for the coordinate input

From TRCCompSci - AQA Computer Science
Revision as of 21:52, 13 February 2017 by Admin (talk | contribs) (Created page with "==Checking coordinates== the InputCoordinate method is within the Simulation class, see below: <syntaxhighlight lang=csharp line> private int InputCoordinate(char Coordinate...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Checking coordinates

the InputCoordinate method is within the Simulation class, see below:

1 private int InputCoordinate(char Coordinatename)
2 {
3 	int Coordinate;
4 	Console.Write("  Input " + Coordinatename + " coordinate: ");
5 	Coordinate = Convert.ToInt32(Console.ReadLine());
6 	return Coordinate;
7 }

This is called everytime a coordinate is required. It can easily be validated because at the LandscapeSize variable could be used. So:


 1 private int InputCoordinate(char Coordinatename)
 2 {
 3         do
 4         {
 5 	      Console.Write("  Input " + Coordinatename + " coordinate: ");
 6 	      Coordinate = Convert.ToInt32(Console.ReadLine());
 7         }
 8 	while (Coordinate < 0 || Coordinate >= LandscapeSize);
 9 	return Coordinate;
10 }

the Coordinate is invalid if it is less than zero or greater than or equal to the LandscapeSize (ie first element is 0).