Hi, I have created a class(Aircraft) that I’m using as a data holder (That stores strings, ints, and enums) and have made a List(Aircraft) of that class. I would like to write a method that returns a new list from List(Aircraft) with the specific variables from Aircraft and its values.
public class AircraftManger : MonoBehaviour
{
public List<Aircraft> aircraftsList = new List<Aircraft>();
public void AddAircraft(string aircraftName, int numberOfWheels, int numberOfWings,
Aircraft.EngineType engineType)
{
aircraftsList.Add(new Aircraft(aircraftName, numberOfWheels, numberOfWings, engineType));
aircraftsList.Sort((a1, a2) => a1.aircraftName.CompareTo(a2.aircraftName));
}
public void RemoveAircraft(string name)
{
foreach (Aircraft a in aircraftsList)
{
if (a.aircraftName == name) aircraftsList.Remove(a);
}
}
**public List<Aircraft> ReturnAircraftsWithName(string name) //Returns a list of <Aircraft> with nam
e.
{
List<Aircraft> returnList = new List<Aircraft>();
foreach (Aircraft a in aircraftsList)
{
if (a.aircraftName == name) returnList.Add(a);
}
return returnList;
}
public List<Aircraft> ReturnAircraftsWithNumberOfWheels(int numberOfWheels) //Returns a list of
<Aircraft> with number of wheels.
{
List<Aircraft> returnList = new List<Aircraft>();
foreach (Aircraft a in aircraftsList)
{
if (a.numberOfWheels == numberOfWheels) returnList.Add(a);
}
return returnList;
}**
}
public class Aircraft //Data Holder class
{
public string aircraftName;
public int numberOfWheels;
public int numberOfWings;
public enum EngineType {Piston, Turbine, Electrical}
public EngineType engineType;
public Aircraft(string aircraftName, int numberOfWheels, int numberOfWings, EngineType
engineType)
{
this.aircraftName = aircraftName;
this.numberOfWheels = numberOfWheels;
this.numberOfWings = numberOfWings;
this.engineType = engineType;
}
}
Now, instead of having to continue with the pattern of creating similar methods like ReturnAircraftsWithName, ReturnAircraftWithNumberOfWings, ReturnAircraftWithNumberOfWheels (Creating the same method for each variable in the Aircraft class) I would like to have a shorthand version of this. Something like:
public List<Aircraft> ReturnAircraftsWithProperty(VariableName value)
{
List<Aircraft> returnList = new List<Aircraft>();
foreach (Aircraft a in aircraftsList)
{
if (a.Variablename == value) returnList.Add(a);
}
return returnList;
}
So that I use a method like this to return a list Aircraft -
List<Aircraft> biplanes = ReturnAircraftsWithProperty(Aircraft.numberOfWings(2));
My actual code has an Aircraft class with over 20 variables so I don’t want to write 20 methods that are basically the same.