Inherits Constructor Method from Base Class?

N

norton

Hello,

May i know if there any ways to inherits a constructor from the base class?

For Example i have a abstract class called User and a Customer class is
inherits from User, and the base class user contains 3 constructor named
1. Public sub new()
2. Public SUb New(ByVal sName as string)
3. Public SUb New(ByVal sName as string, Byval sPassword as string)


When i declare a new customer class, i cannot use new customer(sname) or new
customer(sname, spassword) as constructor, what should i do?

thx for your help in advance

Regards,
Norton
 
R

Robby

You need to define constructors that take the required parameters in your
derieved class and pass them to the constructor of the base class. I
included the parameter-less New() constructor for completeness. If you are
not doing any extra processing in the parameter-less New() constructor then
you do not have to declare it as it is automatically generated for you.

Public Class Customer
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal sName As String)
MyBase.New(sName)
End Sub
Public Sub New(ByVal sName As Atring, _
ByVal sPassword As String)
MyBase.New(sName, sPassword)
End Sub
End Class


Hope this helps

Robby
 
H

Herfried K. Wagner [MVP]

norton said:
May i know if there any ways to inherits a constructor from the base
class?

For Example i have a abstract class called User and a Customer class is
inherits from User, and the base class user contains 3 constructor named
1. Public sub new()
2. Public SUb New(ByVal sName as string)
3. Public SUb New(ByVal sName as string, Byval sPassword as string)

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
///

Inheriting constructors would not make sense because constructors belong to
a certain class. Not inheriting them doesn't break polymorphy. It's not
guaranteed that the derived class is able to be initialized with the data
passed to the base class' constructor, so it's better not to sort-of inherit
them automatically.
 

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