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

MustInherit Class with parameters in the constructor

P: n/a

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 paramstring As String)
mystring = paramstring
End Sub

End Class

When I create the subclass :

Public Class MyChildClass <-- VS warns of a Problem here
Inherits MyParentClass

Public Sub ShowString()
MsgBox(mystring)
End Sub

End Class
VS.NET is complaining I need to declare a "sub new" in the child class
because "The base class doesn't have an accessible 'Sub new' that can be
called without arguments."

I don't understand why this should be necessary, I have no intentions of
creating and instance of this child class without the proper arguments in
New, why should a redefinition of the constructor be required?

Is this a special case for the constructor or am I missing something in how
inheritance works in .net ?

Thanks for any clarification provided,
Francisco


Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Inheritance does not include inheritance of constructors.
So each of your inherited classes will need to have something like:

Public Sub New(ByVal paramstring As String)
MyBase.New(paramstring)
End Sub

"Francisco Amaro" <famaro.at.abacusemedia.dot.com> wrote in message
news:uF**************@tk2msftngp13.phx.gbl...

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 paramstring As String)
mystring = paramstring
End Sub

End Class

When I create the subclass :

Public Class MyChildClass <-- VS warns of a Problem here
Inherits MyParentClass

Public Sub ShowString()
MsgBox(mystring)
End Sub

End Class
VS.NET is complaining I need to declare a "sub new" in the child class
because "The base class doesn't have an accessible 'Sub new' that can be
called without arguments."

I don't understand why this should be necessary, I have no intentions of
creating and instance of this child class without the proper arguments in
New, why should a redefinition of the constructor be required?

Is this a special case for the constructor or am I missing something in how inheritance works in .net ?

Thanks for any clarification provided,
Francisco


Nov 20 '05 #2

P: n/a
"Francisco Amaro" <famaro.at.abacusemedia.dot.com> schrieb

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 paramstring As String)
mystring = paramstring
End Sub

End Class

When I create the subclass :

Public Class MyChildClass <-- VS warns of a Problem here
Inherits MyParentClass

Public Sub ShowString()
MsgBox(mystring)
End Sub

End Class
VS.NET is complaining I need to declare a "sub new" in the child
class because "The base class doesn't have an accessible 'Sub new'
that can be called without arguments."

I don't understand why this should be necessary, I have no intentions
of creating and instance of this child class without the proper
arguments in New, why should a redefinition of the constructor be
required?

The base class constructor *must* be called. Otherwise the object is not
initialized correctly.

Is this a special case for the constructor or am I missing something
in how inheritance works in .net ?

Thanks for any clarification provided,


Add a constructor without arguments to the derived class that calls the base
class constructor.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3

P: n/a

"Marina" <so*****@nospam.com> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
Inheritance does not include inheritance of constructors.
So each of your inherited classes will need to have something like:

Public Sub New(ByVal paramstring As String)
MyBase.New(paramstring)
End Sub

Thanks for the clarification marina.

Any specific reason why it was done ? Seems quite unnecessary to me. Can't
think of a reason why the constructor sub would be an exception in
inheritance.

Will the "Finalize" sub have the same behaviour?

Francisco
"Francisco Amaro" <famaro.at.abacusemedia.dot.com> wrote in message
news:uF**************@tk2msftngp13.phx.gbl...

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 paramstring As String)
mystring = paramstring
End Sub

End Class

When I create the subclass :

Public Class MyChildClass <-- VS warns of a Problem here
Inherits MyParentClass

Public Sub ShowString()
MsgBox(mystring)
End Sub

End Class
VS.NET is complaining I need to declare a "sub new" in the child class
because "The base class doesn't have an accessible 'Sub new' that can be
called without arguments."

I don't understand why this should be necessary, I have no intentions of
creating and instance of this child class without the proper arguments in New, why should a redefinition of the constructor be required?

Is this a special case for the constructor or am I missing something in

how
inheritance works in .net ?

Thanks for any clarification provided,
Francisco



Nov 20 '05 #4

P: n/a
* "Francisco Amaro" <famaro.at.abacusemedia.dot.com> scripsit:
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 paramstring As String)
mystring = paramstring
End Sub

End Class

When I create the subclass :

Public Class MyChildClass <-- VS warns of a Problem here
Inherits MyParentClass

Public Sub ShowString()
MsgBox(mystring)
End Sub

End Class
VS.NET is complaining I need to declare a "sub new" in the child class
because "The base class doesn't have an accessible 'Sub new' that can be
called without arguments."

I don't understand why this should be necessary, I have no intentions of
creating and instance of this child class without the proper arguments in
New, why should a redefinition of the constructor be required?

Is this a special case for the constructor or am I missing something in how
inheritance works in .net ?


The inherited class will need to call the base class' ctor to be
instantiated. For a parameterless ctor, that's easy. If you do not add
a ctor, VB.NET will add a parameterless ctor to your class that calls
the base class' parameterless ctor. If there is no parameterless ctor
defined in the base class, this will fail. That's why you will have to
add a ctor yourself:

\\\
Public Sub New()
MyBase.New("TheParameter")
End Sub
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.