I have an interface Vehicle
implemented by a two classes Car
and Truck
. how do i create an array of interface type containing a mixture of all the different classes and still be able to access their members, and change them
public interface Vehicle
{
double purchase_price();
string vehicle_type();
int release_year();
int purchase_year();
bool IsOld(int releaseYear, int purchaseYear);
void Details();
}
In my main method i have this
var vehls = new Vehicle[]
{
new Truck(8, 1000, "Ford", "F15000", 2011, 2016, false),
new Truck(4, 13000, "Ford", "F-150", 2009, 2014, false),
new Car("Ford", "Super Duty", 2012, 2017, false),
};
when i try to access the car members to assign them it doesn't allow me. it gives me error.
Car c1 = vehls[2];
c1.Wheel = 4;
c1.Miles = 12000;
unless i cast them:
Car c1 = (Car) vehls[2];
c1.Wheel = 4;
c1.Miles = 12000;
How will i be able to create such an array so that i can still be able to access each class member and fields, without filling everything in the constructor. I don't want to be casting it all the time. Is there a way that the assignment will be able to detect the class type automatic and allow me to access and assign members?