Method
In this section, we will learn how to use method accross the class.
Let's back to our Map class, we need to have a function to validate whether the point is on the map or not. We should use function that return boolean (true or false), we called it onMap()
function.
public bool onMap(Point point)
{
var inBounds = point.X >= 0 && point.X < Width && point.Y >= 0 && point.Y < Height;
return inBounds;
}
Try to use this function in Game.cs
class Game
{
static void Main(string[] args)
{
Map map = new Map(8, 5);
Point p = new Point(4, 2);
bool isOnMap = map.onMap(p);
Console.WriteLine(isOnMap);
p = new Point(8, 5);
isOnMap = map.onMap(p);
Console.WriteLine(isOnMap);
Console.Read();
}
}
WriteLine from Console class is static method.
onMap from map class is instance method.