In addition to Caius Jard's answer I fiddled something like this:
I made a class called Airport:
namespace Airport
{
public class Airport
{
// Properties of Airport object:
public int Id { get; set; }
private List<Plane> Planes { get; set; }
// Constructor method for Airport object:
public Airport(int id, List<Plane> planes)
{
Id = id;
Planes = planes;
}
// A method to remove planes from the airport's collection (just an example of how OOP could be used to make "readable" code):
public void RemoveDepartedPlane(int id)
{
if(Planes.Exists(x => x.Id == id))
{
Planes.Remove(Planes.Find(x => x.Id == id));
}
Console.WriteLine("A plane by id: {0} has been removed from available planes on this airport", id);
}
// A method to add arrived planes to the airport's collection:
public void AddArrivedPlane(int id)
{
// Custom made class called Coordinate:
Coordinate coordinate = new Coordinate(0, "Airportlocation");
// Custom made class called GeolocationSystem (which I just came up with):
GeolocationSystem geolocationSystem = new GeolocationSystem(0, "", "", DateTime.Now, coordinate);
Planes.Add(new Plane(id, "", "", "", geolocationSystem));
}
// Gets the amount of used up spaces of planes on current airport
public int GetUsedSpaces()
{
return Planes.Count();
}
}
}
As you can see, it might be able to contain a list of planes rather than a collection of strings. Using a plane as object rather than a string would be a more powerful way to handle your logic, as you can set properties and make methods for the plane instead of making tons and tons of variables (like you would in plain JavaScript) doing the same for the purpose of achieving the handling of planes.
Also (in my opinion and I think many others too), it reads easier and makes more sense when going through OOP (object oriented programming) code. In JavaScript you could also make objects to achieve similar things, but that would be a different subject.
The Plane class could look something like this:
namespace Airport
{
public class Plane
{
public int Id { get; set; }
public string Type { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public GeolocationSystem GeolocationSystem { get; set; }
public Plane(int id, string name, string type, string color, GeolocationSystem geolocationSystem)
{
Id = id;
Name = name;
Type = type;
Color = color;
GeolocationSystem = geolocationSystem;
}
// All sorts of methods belonging to a plane:
public void TakeOff()
{
Console.WriteLine("Taking off!");
}
public void AutoPilot()
{
Console.WriteLine("Autopiloting...");
}
public void Land()
{
Console.WriteLine("Now landing...");
}
public void TurnLeft()
{
Console.WriteLine("Turning left.");
}
public void TurnRight()
{
Console.WriteLine("Turning right.");
}
public void Ascend()
{
Console.WriteLine("Ascending.");
}
public void Descend()
{
Console.WriteLine("Descending.");
}
}
}
And maybe if you want to add an object as property to another object, this is how you could do it per my custom made GeolocationSystem class:
namespace Airport
{
public class GeolocationSystem
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public DateTime TimeStamp { get; set; }
public Coordinate Coordinate { get; set; }
public GeolocationSystem(int id, string name, string type, DateTime timeStamp, Coordinate coordinate)
{
Id = id;
Name = name;
Type = type;
TimeStamp = timeStamp;
Coordinate = coordinate;
}
public void GetLocation()
{
Console.WriteLine("Location of current system is at coordinate: {0}, at time: {1}.", Coordinate.Value, TimeStamp);
}
public void GetSystemInfo()
{
Console.WriteLine("System name: {0} {1} and is type: {2}.", Id, Name, Type);
}
}
}
Then if you would like to call methods and/or create object instances of the classes, you could do something like this:
namespace Airport
{
class Program
{
static void Main(string[] args)
{
// Create any object required for making an instance object of the airport:
List<Plane> planes = new List<Plane>();
// Then create an instance of the airport:
Airport airport = new Airport(0, planes);
// Now that the airport exists (as an object), you could now access its properties and/or methods and set and/or use these:
// Set Planes in airport's plane collection:
for (var i = 0; i < 10; i++)
{
airport.AddArrivedPlane(i);
}
// "Read" the collection:
Console.WriteLine(airport.GetUsedSpaces());
// Or you could play around with the Plane class (or an object of that type):
Plane plane = new Plane(0, "", "", "" , new GeolocationSystem(0, "", "", DateTime.Now, new Coordinate(0, "")));
plane.TakeOff();
Console.ReadKey(true);
}
}
}
This is just some example code to show how you could achieve whatever you are trying to achieve. If you need any help figuring out how my example code works or need any other advice regarding OOP C#, you could leave a comment.
voidandstringshould not be combined for a method. Either you return nothing and it is just a method (a.k.a. a function like a JavaScript function) or you return something like a number (which would return as anint(integer) in C#) or a piece of text (which would return as astringin C#). In the example you provided you are not returning a string, so you might want to consider removingstringfrompublic void string Land(object plane).string planes[]should be declared outside the class constructor.voidkeyword alone for creating (void) methods until you learn to understand how to return certain types (like string and integer) to keep things simple.