Two classes:
public class _A {
public int Value1;
public int Value2;
public _A(int v1, int v2) {
this.Value1 = v1;
this.Value2 = v2;
}
}
public class _B : _A {
public int Value3;
public _B(_A a, v3) : base(a.Value1, a.Value2) {
this.Value3 = v3;
}
}
Is there a way I can create a constructor for _B without have to
break apart the elements of the _A typed argument when calling
the base constructor?
Jon Skeet [C# MVP] - 28 Jun 2007 22:11 GMT
> Two classes:
>
[quoted text clipped - 19 lines]
> break apart the elements of the _A typed argument when calling
> the base constructor?
I'm not sure what you mean - could you elaborate?

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Peter Duniho - 28 Jun 2007 23:16 GMT
>> Is there a way I can create a constructor for _B without have to
>> break apart the elements of the _A typed argument when calling
>> the base constructor?
>
> I'm not sure what you mean - could you elaborate?
I assume he means he'd like to do "public _B(_A a, v3) : base(a)..." or
something similar even when _A doesn't define a constructor that takes an
instance of an _A.
As far as I know, it's not possible. But I've been wrong before. :)
Pete
Jon Skeet [C# MVP] - 28 Jun 2007 23:30 GMT
> >> Is there a way I can create a constructor for _B without have to
> >> break apart the elements of the _A typed argument when calling
[quoted text clipped - 7 lines]
>
> As far as I know, it's not possible. But I've been wrong before. :)
Correct. On the other hand, it's perfectly possible for a constructor
to take an instance of the same type and copy field values over etc.
What you also can't do is try to create a new object which still uses
the old object for its base fields, permanently linking the two
together. Of course, you can keep a reference to the old object and
keep referring to it that way, using composition instead of (or as well
as) inheritance.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jamie Risk - 29 Jun 2007 14:29 GMT
> What you also can't do is try to create a new object which still uses
> the old object for its base fields, permanently linking the two
> together.
This exactly what I was trying to ask.
Peter Duniho - 29 Jun 2007 17:28 GMT
>> What you also can't do is try to create a new object which still uses
>> the old object for its base fields, permanently linking the two
>> together.
>
> This exactly what I was trying to ask.
Really? That wasn't clear _at all_, for what it's worth. Your example
appeared to be asking simply about copying the values from the old object,
not tying the old object permanently to the new one.
Chris Dunaway - 28 Jun 2007 22:32 GMT
> Two classes:
>
[quoted text clipped - 21 lines]
> break apart the elements of the _A typed argument when calling
> the base constructor?
Add a constructor to your A class that takes an instance of A:
public class _A {
> public int Value1;
> public int Value2;
[quoted text clipped - 3 lines]
> this.Value2 = v2;
> }
//second constructor
public _A(_A a) {
}
>}
Then you can change your _B constructor like this:
> public class _B : _A {
> public int Value3;
[quoted text clipped - 4 lines]
>
> }
I hope I've understood your question correctly.
Chris