I have tried to add two classes (car and van) in an arraylist but seems like the list is defined by the first class (car) and when trying to add the other class (van) I get the runtime error
System.InvalidCastException: 'Unable to cast object of type 'CarBroker.Car' to type 'CarBroker.Van'.
I believe the error is caused by the arraylist is defined when creating the first entry of the type 'Car' and when trying to add a different type 'Van' the error occurs.
Is there another list that can be used instead?
public class List // Global ArrayList to store car data
{ public static ArrayList vehicle = new ArrayList(); }
static void Main(string[] args)
{
Car car = new Car();
car.SetVehicleData();
car.SetOwnerData();
List.vehicle.Add(car);
Van van = new Van();
van.SetVehicleData();
van.SetCompanyData();
List.vehicle.Add(van);
Admin a1 = new Admin();
a1.ListAll();
Console.ReadKey();
}
When running my ListAll() method the ideal output would be first list entry with the class 'car' and the second entry with the class 'van'
Instead I get the error mentioned above.
Anyone that can help me on the way using the right list or another function? I am attending a basic C# class so I am not that advanced :)