Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > List<IVehicle> objCars=list<Car> where{ Car:IVehicle} does not work, Why?
 

List<IVehicle> objCars=list<Car> where{ Car:IVehicle} does not work, Why?

I have a problem here..

I expect

List<IVehicle> objCars = new List<Car>();

to work just like

IVehicle objCar = new Car();

but it shows error.

What has gone wrong? Explanations would be greatly appreciated..

Thanks

Kiran

Code below for a Console Application

//--------------------------------------------------

using System;

using System.Collections.Generic;

using System.Text;

namespace Interface_in_List_Generic_Test

{

interface IVehicle

{

void Start();

void ApplyBreaks();

}

class Car : IVehicle

{

public void Start()

{ Console.WriteLine("Starting Car"); }

public void ApplyBreaks()

{ Console.WriteLine("Car Decelerating..."); }

}

class Program

{

static void Main(string[] args)

{

//Below Code Works....

IVehicle objCar = new Car();

objCar.ApplyBreaks();

//Below code shows error-> Cannot implicitly convert type...

List<IVehicle> objCars = new List<Car>();

}

}

}

/*Cannot implicitly convert type

'System.Collections.Generic.List<Interface_in_List_Generic_Test.Car>' to 'System.Collections.Generic.List<Interface_in_List_Generic_Test.IVehicle>' C:\KVR\KVR Devs\comboBoxSample\Interface in List Generic Test\Program.cs 33 38 Interface in List Generic Test*/

Kiran Raveendranathan  Thursday, December 04, 2008 1:25 PM

Hi Kiran Raveendranathan,

You need to change the code into this one.

Code Snippet

<IVehicle> objCars = new List<IVehicle>();

objCar = new Car();

If I have misunderstood you, please feel free to tell me.

Sincerely,

Kira Qian

Kira Qian  Monday, December 08, 2008 3:19 AM

Hi Kira,

Thanks for the response.Yes it works for sure butI was asking something different...

-----------------------------------------------------------------------------

IVehicle objVehicle;

Car objCar;

When we are able to assign objCar to objVehicle

as ObjVehicle=objCar;

Why we are not able to perform objVehicleList=objCarList?

Where, List<IVehicle> objVehicleList

List<Car> objCarList

(assume, object initialisations are done and Car implements IVehicle)

I feel that the possible explanation may be

List<Car>does not implement List<IVehicle>, So can not be equated. (even if car implements IVehicles)

Can someone please explain/confirm it?

Kiran Raveendranathan  Wednesday, December 10, 2008 12:34 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView Scrollbar
• Migration of VB 6.0 MS-Grid control to VB.NET causing problems
• Why Combobox DropDownWidth is ignored in DataGridView
• DataGridView Colums renders in the wrong order
• DataBinding TextBox to ComboBox
• Displaying Data from Database
• DGV issue with combos.
• How do I call this.tableadapter.update without knowing which table adapter?
• Combo Boxes Bound To Objects Only Sometimes Update
• Directory Polling with Windows Service