473,507 Members | 8,054 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inherits Constructor Method from Base Class?

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
Nov 21 '05 #1
2 4697

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

"norton" <no********@hotmail.com> wrote in message
news:u2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #2
"norton" <no********@hotmail.com> schrieb:
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.

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

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

Similar topics

6
4974
by: cppaddict | last post by:
Hi, I know that C++ does not have an explicit super() constructor for calling a Base class constructor from a Derived class's constructor, but my understanding is that C++ implements this...
19
3544
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
3
11810
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
45
6309
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
10
2292
by: Gniluy | last post by:
Hi, I have a protected method that I only want it to be called from code in constructor or from methods called by constructor. If it is not called directly or indirectly from constructor, I want...
6
2469
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
23
7199
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
2
1667
by: Ethan Strauss | last post by:
Hi, I want to be able to make a Master constructor for a class which all overloads of the class constructor would call and thus if I have minor changes to something I only need to make them in the...
7
1992
by: =?ISO-8859-1?Q?Fernando_G=F3mez?= | last post by:
Hello all. I have this class with a virtual method and a constructor that calls this virtual method. A derived class overrides this virtual method, so I expected that when the base's constructor is...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.