I have the following classes.
Car.cs
public class Car
{
}
scoda.cs
public class scoda : Car
{
}
Test.cs
public class Test
{
public ObservableCollection<scoda> scodaList;
public Test()
{
scodaList = new ObservableCollection<scoda>();
scoda s = new scoda();
scodaList.Add(s);
set(scodaList);
}
public void set(ObservableCollection<Car> list)
{
}
}
I got the casting error when calling set method as below
Cannot convert from 'System.Collections.ObjectModel.ObservableCollection<KillCarMain.deneme.scoda>' to 'System.Collections.ObjectModel.ObservableCollection<KillCardMain.deneme.Car>'
How to fix this problem ?
CodePudding user response:
As pointed out in the comment, a collection of Car
is not a collection of Scoda
.
So you should convert your collection of Scoda
into a collection of Car
:
var carList = new ObservableCollection<Car>(scodaList.Select(s => (Car)s));
set(carList);
If you don't want to create a new ObservableCollection
, you could define scodaList as a collection of Car
objects:
public ObservableCollection<Car> scodaList;
private void test()
{
scodaList = new ObservableCollection<Car>();
scoda s = new scoda();
scodaList.Add(s);
set(scodaList);
}
public void set(ObservableCollection<Car> list)
{
}