Class inheritance constructor questino ...

J

Jamie Risk

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?
 
J

Jon Skeet [C# MVP]

Jamie Risk said:
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?

I'm not sure what you mean - could you elaborate?
 
C

Chris Dunaway

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?

Add a constructor to your A class that takes an instance of A:

public class _A {
public int Value1;
public int Value2;

public _A(int v1, int v2) {
this.Value1 = v1;
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;

public _B(_A a, v3) : base(a) {
this.Value3 = v3;
}

}


I hope I've understood your question correctly.

Chris
 
P

Peter Duniho

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
 
J

Jon Skeet [C# MVP]

Peter Duniho said:
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. :)

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.
 
J

Jamie Risk

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.
 
P

Peter Duniho

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top