Take the 2-minute tour ×

I have a program that has a superclass vehicle and has 3 classes inheriting from it car,truck,minibus I need to take instnaces of the classes and display them in a listbox on my main form 'FrmHire' I have a class called fleet for this.

I keep getting this error, Cannot implicitly convert type App1.Vehicle to App1.Car. An explicit conversion exists (are you missing a cast?)

private void lstFleet_SelectedIndexChanged(object sender, EventArgs e)
         * This method is used to control the list box
         * It is called when a row is selected by the user, it then displays frmCar
         * with the car details
        if (lstFleet.SelectedIndex > -1)
            int index = lstFleet.SelectedIndex;
            Car myCar = myFleet.fleet.ElementAt(index);
            FrmCar carGui = new FrmCar();
            carGui.car = myCar;
share|improve this question
Can you point to the line that throws an error? –  T.S. Dec 17 at 20:19

4 Answers 4

Given that your list might contain Cars, Trucks or Minibusses, you can assume that you can take the ElementAt(index) and cast it to Car, can you?

Presumably you get back a Vehicle from the ElementAt(index), and if you know that it will be a car then you can explicitly cast it to Car, but the implicit cast that you have here is causing this issue.

So, you either need (if you know for sure they're all Car instances and anything else is an exception)...

Car myCar = (Car)myFleet.fleet.ElementAt(index);

... or you need to deal with this as a Vehicle...

Vehicle myVehicle = myFleet.fleet.ElementAt(index);

If you want to test whether the item is a Car (and do something different if not) then this...

Car myCar = myFleet.fleet.ElementAt(index) as Car;
if (myCar != null)
    // it was a car
share|improve this answer
the first one works but in the listbox it throws in the name of the solution then a .then car instead of Car:? any ideas? –  bettsy583 Dec 2 '10 at 21:23

What is the type of myFleet.fleet? I suspect it is a collection of Vehicle's and you're trying to assign one to a Car.

share|improve this answer
Yes that's right! –  bettsy583 Dec 2 '10 at 21:21
Car myCar = (Car)myFleet.fleet.ElementAt(index);

This will throw an exception if myFleet.fleet.ElementAt(index); is not a Car. To check if it is a Car you can do if (myFleet.fleet.ElementAt(index) is Car) {...}. But the best is to use a List<Car> instead of List<Vehicle> if you're only going to store Cars in it.

share|improve this answer
This might throw. I'd prefer myFleet.fleet.ElementAt(index) as Car so I can check myCar for null. –  n8wrl Dec 2 '10 at 21:18
@n8wrl, I noted that in my edit. –  Albin Sunnanbo Dec 2 '10 at 21:20
Hi has removed the error but in my text box it says 'WindowsApplication1.Car' apose to 'Car: which I have it should do.... –  bettsy583 Dec 2 '10 at 21:20
@bettsy583, on what line? Is that on the carGui.car = myCar; line now? –  Albin Sunnanbo Dec 2 '10 at 21:22
@bettsy583: Are you assinging a property of a vehicle to your textbox? Sounds like you're using ToString() which by default will show the class name. –  n8wrl Dec 2 '10 at 21:23

Try this:

Car myCar = (Car)myFleet.fleet.ElementAt(index);
share|improve this answer
This works but it takes the name of the solution and displays it in the listbox..... –  bettsy583 Dec 2 '10 at 22:01

Your Answer


