3

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.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
csharpnoob
  • 505
  • 1
  • 9
  • 24
  • That seems wrong, should be the other way around, if not, why would you want to cast a Car to MyCar? –  May 10 '10 at 11:09
  • Because 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. – csharpnoob May 10 '10 at 11:11
  • @csharpnoob - I've added the content of your comment to the question to help you get the best solution for this particular problem without getting bogged down in the OOP stuff. – cjk May 10 '10 at 11:20
  • Seems like you are trying to combine domain objects with data objects. That is wrong. If I misunderstood your question, and that is not the case. Then modify your table/database schema, or separate data objects from domain/business objects. So each set of them will have their own members as appropriate. Look for the Data Mapper pattern. You need not to use inheritance here. –  May 10 '10 at 11:45
  • Thats the problem i can't modify the tables/database schema. They need to stay. But i need additional attributes. But adding them in the VS Linq-to-sql designer doesn't look like a good solution. Extending these objects works fine. It just how to fill the new object with the linq-to-sql without having all the manual work in constructor. Just cast loaded to extended class. Fill up the rest by application. Everything is working except the cast for the same attributes/datatypes leaving out my addtional. Thinking look of Attributes of both object with reflections. – csharpnoob May 10 '10 at 12:00
  • I changed the title of the question to more accurately reflect the underlying problem since the answer has nothing to do with subclassing. :) – Sam Harwell May 10 '10 at 13:33

4 Answers4

17

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.

cjk
  • 45,739
  • 9
  • 81
  • 112
  • 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 May 10 '10 at 11:21
  • 1
    You might not have understood ck's answer. If you define the following: public partial class Car { public List 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 May 10 '10 at 12:41
  • +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 May 10 '10 at 13:28
  • 1
    This is the answer to the real question here. – Sam Harwell May 10 '10 at 13:29
2

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
  • 55,237
  • 33
  • 144
  • 193
1

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
  • 32,535
  • 13
  • 101
  • 181
  • 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 May 10 '10 at 11:18
  • ok. you should really have asked the question you wanted answered, not some other related question. would have saved everybody some time. – Sam Holder May 10 '10 at 11:27
  • Why? Does the question not point to a possible solution? It's just a bit more general? – csharpnoob May 10 '10 at 11:40
0

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
  • 43,366
  • 8
  • 94
  • 151