By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Sub class constructor

P: n/a
Hi:

The parent class have 3 version of constructor:

public class Parent
public sub New()
end sub

public sub New(i as integer)
myclass.New()
...
end sub

public sub New(i as integer, s as string)
myclass.New()
...
end sub
end class
public class Child
inherits Parent

end class
When i try to create the Child object, i can only use the New() as a
constructor, the others 2 constructor is not accessable, what am i
missing ?

Thanks
JCVoon


Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
MN
If you want to use parent constructors when instanciating your child class,
you need to implement the 3 constructors in your child class and call parent
constructor from it;

public class Parent
public sub New()
MyBase.New
...
end sub

public sub New(i as integer)
MyBase.New(i)
...
end sub

public sub New(i as integer, s as string)
MyBase.New(i, s)
...
end sub

...
end class
Note that when you instantiate a class, you are calling the class
constructors not parent class constructor;

Regards.
"JC Voon" <jc*******@yahoo.com> a écrit dans le message de news:
41***************@msnews.microsoft.com...
Hi:

The parent class have 3 version of constructor:

public class Parent
public sub New()
end sub

public sub New(i as integer)
myclass.New()
...
end sub

public sub New(i as integer, s as string)
myclass.New()
...
end sub
end class
public class Child
inherits Parent

end class
When i try to create the Child object, i can only use the New() as a
constructor, the others 2 constructor is not accessable, what am i
missing ?

Thanks
JCVoon


Nov 21 '05 #2

P: n/a
"JC Voon" <jc*******@yahoo.com> schrieb:
The parent class have 3 version of constructor:

public class Parent
public sub New()
end sub

public sub New(i as integer)
myclass.New()
...
end sub

public sub New(i as integer, s as string)
myclass.New()
...
end sub
end class
public class Child
inherits Parent

end class
When i try to create the Child object, i can only use the New() as a
constructor, the others 2 constructor is not accessable, what am i
missing ?


Constructors are not inherited by the base class. You'll have to add the
constructors to the derived class too, but you can simply call the base
class' ctor there:

\\\
Public Class Base
Public Sub New(ByVal i As Integer, ByVal s As String)
...
End Sub
End Class

Public Class Derived
Inherits Base

Public Sub New(ByVal i As Integer, ByVal s As String)
MyBase.New(i, s)
End Sub
End Class
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:u9*************@TK2MSFTNGP14.phx.gbl...
Constructors are not inherited by the base class. You'll have to add the
constructors to the derived class too


Herfried,
<question type='probably silly'>

Perfectly understandable and I've done it myself often enough, but
doesn't this go against the "Rules" of Polymorphism?

All /other/ methods are inherited, why /not/ the Constructors as well?

TIA,
Phill W.
Nov 21 '05 #4

P: n/a
"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> schrieb:
Constructors are not inherited by the base class. You'll have to add the
constructors to the derived class too


<question type='probably silly'>

Perfectly understandable and I've done it myself often enough, but
doesn't this go against the "Rules" of Polymorphism?

All /other/ methods are inherited, why /not/ the Constructors as well?


A subclass is not necessarily able to be created with the data that is
passed to its base class only. Maybe additional data needs to be passed to
the class' constructor. In addition to that, you always call a construtor
on a specific type, so there is no real polymorphism.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5

P: n/a
MN, Herfried K.Wagner :

Thanks for the reply.
I missed the inherited and overridable constructor in Delphi...:(
Does it apply to C# too ?

Thanks
JCVoon
Nov 21 '05 #6

P: n/a
"JC Voon" <jc*******@yahoo.com> schrieb:
Thanks for the reply.
I missed the inherited and overridable constructor in Delphi...:(
Does it apply to C# too ?


Yes.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.