Difference between revisions of "Validation for menu options for the coordinate input"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "==Checking coordinates== the InputCoordinate method is within the Simulation class, see below: <syntaxhighlight lang=csharp line> private int InputCoordinate(char Coordinate...")
 
(Checking coordinates)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
==Checking coordinates==
 
==Checking coordinates==
  
the InputCoordinate method is within the Simulation class, see below:
+
The InputCoordinate method is within the Simulation class, see below:
  
 
<syntaxhighlight lang=csharp line>
 
<syntaxhighlight lang=csharp line>
Line 12: Line 12:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  
 
This is called everytime a coordinate is required. It can easily be validated because at the LandscapeSize variable could be used. So:
 
This is called everytime a coordinate is required. It can easily be validated because at the LandscapeSize variable could be used. So:
 
  
 
<syntaxhighlight lang=csharp line>
 
<syntaxhighlight lang=csharp line>
Line 29: Line 29:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
the Coordinate is invalid if it is less than zero or greater than or equal to the LandscapeSize (ie first element is 0).
+
 
 +
The Coordinate is invalid if it is less than zero or greater than or equal to the LandscapeSize (ie first element is 0).

Latest revision as of 22:53, 13 February 2017

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