473,379 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Class inheritance constructor questino ...

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?
Jun 28 '07 #1
6 1314
Jamie Risk <risk.#.@intectus.comwrote:
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?

--
Jon Skeet - <sk***@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
Jun 28 '07 #2
On Jun 28, 4:02 pm, Jamie Risk <risk...@intectus.comwrote:
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

Jun 28 '07 #3
On Thu, 28 Jun 2007 14:11:42 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
>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
Jun 28 '07 #4
Peter Duniho <Np*********@nnowslpianmk.comwrote:
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. :)
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.

--
Jon Skeet - <sk***@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
Jun 28 '07 #5
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.
Jun 29 '07 #6
On Fri, 29 Jun 2007 06:29:11 -0700, Jamie Risk <risk.#.@intectus.com>
wrote:
>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.
Jun 29 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
7
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
8
by: Nick | last post by:
I have the following code: var obj = {a:0, b:1, ...} function f() {...} obj.f = f // This will make a instance method? How to make a Class method for the class of obj? Or what's the...
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
4
by: Francisco Amaro | last post by:
Hi all, Have question about inheriting a class that has parameters in the constructor such as : Public MustInherit Class MyParentClass Public mystring As String Public Sub New(ByVal...
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
5
by: petethebloke | last post by:
I'm not sure how to ask for what I want, so I can't find it on Google. I'll explain..... I have a class 'hanging' with a constructor: class hanging{ function hanging(){ }
11
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by...
6
by: Xu, Qian | last post by:
Hello All, is there any handy tool to generate class diagrams for javascript? I have tried JS/UML, but it generates always an empty diagram. -- Xu, Qian (stanleyxu)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.