views:

60

answers:

1

How do something like that:

Class Car: string Name, int year
Class SUV:Car;

SUV bmw=ToCar(car);

Something like that ??

public object ToObject(Type type, Car car)
{
// here i just wanna take string and int pool
}

I did something like:

List<Car> cars=new List<Car>

SUV suv=new SUV();

cars.Add(suv).

And next I want:

SUV suv=(Suv)cars[0];

But I get an error that this casting is wrong. :/

I want to just recover the SUV.

+3  A: 

It looks like you've got the basic idea, there were some naming errors in your example that I'm assuming were just typos and not what the actual code looks like...something like this works fine which is what it looks like you are getting at:

static void Main(string[] args)
        {
            List<Car> cars = new List<Car>();
            SUV suv = new SUV { name = "mySuv", year = 2010 };
            cars.Add(suv);

            SUV anotherSuvReference = (SUV)cars[0];
            Console.WriteLine(anotherSuvReference.name);
        }

        class Car
        {
            public string name;
            public int year;
        }
        class SUV : Car
        {
        }
kekekela