views:

133

answers:

4

Hi,


Update from comment:

I need to extend linq-to-sql classes by own parameters and dont want to touch any generated classes. Any better suggestes are welcome. But I also don't want to do all attributes assignments all time again if the linq-to-sql classes are changing. so if vstudio generates new attribute to a class i have my own extended attributes kept separate, and the new innerited from the class itself


Original question:

i'm not sure if it's possible. I have a class car and a class mycar extended from class car. Class mycar has also a string list. Only difference.

How can i cast now any car object to a mycar object without assigning all attributes each by hand. Like:

Car car = new Car();

MyCar mcar = (MyCar) car;

or

MyCar mcar = new MyCar(car);

or however i can extend car with own variables and don't have to do always

Car car = new Car();
MyCar mcar = new MyCar();
mcar.name = car.name;
mcar.xyz = car.xyz;
...

Thanks.

+1  A: 

you can't definately cast a Car as MyCar because there is no guarantee that the Car is a MyCar. You can try the cast and see if it works, or you can use the as keyword to try the cast and get null if it fails.

How are the properties set in Car? Why can't MyCar just use the same technique for setting its properties? After all it is a Car. Why do you want to create a MyCar from a Car? Why not just create a MyCar in the first place?

You could also create a constructor in MyCar which takes a Car and assign the properties of MyCar to those of Car in the constructor.

You might also consider using the decorator pattern as an alternative to subclassing, and have your MyCar delegate the calls to the wrapped Car

Sam Holder
thanks a lot for your comment. see my comment in the question. it's about getting data in the innerited object from linq-to-sql
csharpnoob
ok. you should really have asked the question you wanted answered, not some other related question. would have saved everybody some time.
Sam Holder
Why? Does the question not point to a possible solution? It's just a bit more general?
csharpnoob
+2  A: 

You can create a copy constructor accepting a base class parameter in the derived class:

class MyCar {
    public MyCar(Car car) {
        name = car.name;
        // etc
    }
}
Paolo Tedesco
+7  A: 

In response to your comment on the question, the Linq-To-Sql classes are generated as partial. This means you can have separate code files with the same class names declared as partial to add the extra properties you want.

E.g. Your Ling-To-Sql designer class will be:

public partial class Car
{
     .... lots of auto generated stuff ....
}

You can have your own separate file (in the same project) called Car.cs:

public partial class Car
{
     public MyCustomStuff{ get; set; }
}

And the two classes will be merged together. Just make sure they are in the same namespace.

ck
thats why I create new class and extented the linq class. only thing missing is how to put now the data into the new objects without assigning all attributes manual in constructor.
csharpnoob
You might not have understood ck's answer. If you define the following:public partial class Car { public List<string> MyProperty1 { get; set; }}You will have exactly what you wanted. You aren't modifying the generated class, you are extending it without subclassing it. Think of WinForms generated classes, then you write your own customisations in a separate file that gets pushed together at compile time.
Josh Smeaton
+1. Generated classes, like linq2Sql classes should not be derived from to add functionality. They should be tacked on to using partial so you don't need to deal with subclassing headaches.
Brian Genisio
This is the answer to the real question here.
280Z28
A: 

There are several options you can use:

  • implicit or explicit operator
    public static explicit operator Car(MyCar source)

  • a copy constructor
    public MyCar(Car source)

  • an extension method
    public static class CarExtensions {
    public static MyCar Create(this Car source) }

Oliver