MustInherit Class with parameters in the constructor

F

Francisco Amaro

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
 
M

Marina

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
 
A

Armin Zingler

Francisco Amaro said:
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
 
F

Francisco Amaro

Marina said:
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
 
H

Herfried K. Wagner [MVP]

* "Francisco Amaro said:
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
///
 

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