473,749 Members | 2,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloaded New in MustInherit Class ...

Hello -

I have a question regarding overloading the New() in a MustInherit
Class. The following show the code in question ...

MustInherit Class cAbstract
Public MustOverride Sub print()

Protected mName As String

Public Sub New() ' Why do I have to override this?
mName = "N/A"
End Sub

Public Sub New(ByVal aName As String)
mName = aName
End Sub
End Class

Class cChildOfAbstrac t
Inherits cAbstract

Public Overrides Sub print()
Console.WriteLi ne("cChildOfAbs tract.print() mName = '" &
mName & "'")
End Sub
End Class

....
Dim lInstanceOfChil d As New cChildOfAbstrac t("My Name") ' Why is
this not working?
....

First of all, I have to override the New(). Is this because I
overloaded it with the New(ByVal aName As String)?

Then I'm trying to call the overloaded constructor when I instantiate
the cChildOfAbstrac t but I cannot do that because apparently I have
too many arguments. This works if I override the New(ByVal aName As
String) in the child class but why do I have to do that? I thought I
would inherit that from the base class.

Thanks!
Joe
Oct 27 '08 #1
6 4169
What do you mean by 'have to override the New()' - what is the message you
are getting? Constructors aren't ordinary methods and are inherited
differently.

If the base class has no constructors or has a a paramaterless constructor
then you don't need a constructor. But if the base class does not have a
paramaterless constructor then your derived class must have a constructor
and the very first line of the constructor must be a call to the base class
constructor. Perhaps it was this requirement (not met in your sample code)
that made it appear that you were required to have a New(). It all depends
on what the base class's constructors are.

"Joe HM" <un*******@yaho o.comwrote in message
news:e0******** *************** ***********@r66 g2000hsg.google groups.com...
Hello -

I have a question regarding overloading the New() in a MustInherit
Class. The following show the code in question ...

MustInherit Class cAbstract
Public MustOverride Sub print()

Protected mName As String

Public Sub New() ' Why do I have to override this?
mName = "N/A"
End Sub

Public Sub New(ByVal aName As String)
mName = aName
End Sub
End Class

Class cChildOfAbstrac t
Inherits cAbstract

Public Overrides Sub print()
Console.WriteLi ne("cChildOfAbs tract.print() mName = '" &
mName & "'")
End Sub
End Class

...
Dim lInstanceOfChil d As New cChildOfAbstrac t("My Name") ' Why is
this not working?
...

First of all, I have to override the New(). Is this because I
overloaded it with the New(ByVal aName As String)?

Then I'm trying to call the overloaded constructor when I instantiate
the cChildOfAbstrac t but I cannot do that because apparently I have
too many arguments. This works if I override the New(ByVal aName As
String) in the child class but why do I have to do that? I thought I
would inherit that from the base class.

Thanks!
Joe
Oct 27 '08 #2
To the best of my knowledge, constructors are not inheritted in VB.Net. Add
this constructor to your class cChildOfAbstrac t to accomplish what I think
you want:

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

I don't think you need the default constructor in your base class, at least
in what I was running.

"Joe HM" <un*******@yaho o.comwrote in message
news:e0******** *************** ***********@r66 g2000hsg.google groups.com...
Hello -

I have a question regarding overloading the New() in a MustInherit
Class. The following show the code in question ...

MustInherit Class cAbstract
Public MustOverride Sub print()

Protected mName As String

Public Sub New() ' Why do I have to override this?
mName = "N/A"
End Sub

Public Sub New(ByVal aName As String)
mName = aName
End Sub
End Class

Class cChildOfAbstrac t
Inherits cAbstract

Public Overrides Sub print()
Console.WriteLi ne("cChildOfAbs tract.print() mName = '" &
mName & "'")
End Sub
End Class

...
Dim lInstanceOfChil d As New cChildOfAbstrac t("My Name") ' Why is
this not working?
...

First of all, I have to override the New(). Is this because I
overloaded it with the New(ByVal aName As String)?

Then I'm trying to call the overloaded constructor when I instantiate
the cChildOfAbstrac t but I cannot do that because apparently I have
too many arguments. This works if I override the New(ByVal aName As
String) in the child class but why do I have to do that? I thought I
would inherit that from the base class.

Thanks!
Joe
Oct 27 '08 #3
On Oct 27, 9:30 am, Joe HM <unixve...@yaho o.comwrote:
Hello -

I have a question regarding overloading the New() in a MustInherit
Class. The following show the code in question ...

MustInherit Class cAbstract
Public MustOverride Sub print()

Protected mName As String

Public Sub New() ' Why do I have to override this?
mName = "N/A"
End Sub

Public Sub New(ByVal aName As String)
mName = aName
End Sub
End Class

Class cChildOfAbstrac t
Inherits cAbstract

Public Overrides Sub print()
Console.WriteLi ne("cChildOfAbs tract.print() mName = '" &
mName & "'")
End Sub
End Class

...
Dim lInstanceOfChil d As New cChildOfAbstrac t("My Name") ' Why is
this not working?
...

First of all, I have to override the New(). Is this because I
overloaded it with the New(ByVal aName As String)?

Then I'm trying to call the overloaded constructor when I instantiate
the cChildOfAbstrac t but I cannot do that because apparently I have
too many arguments. This works if I override the New(ByVal aName As
String) in the child class but why do I have to do that? I thought I
would inherit that from the base class.

Thanks!
Joe
Constructors are not inherited. You will have to provide the child
constructors that you need.

Chris
Oct 28 '08 #4
Hello -

The following will cause a "Class 'cChild' must declare a 'Sub New'
because its base class 'cParent' does not have an accessible 'Sub New'
that can be called with no arguments."

MustInherit Class cParent
Public Sub New(ByVal aArgument As String)
MyBase.New()
End Sub
End Class

Class cChild
Inherits cParent
End Class

I thought that by default every class has a parameterless constructor
but I guess I was mistaken. Looks like I have to spell it out.

Thanks,
Joe

On Oct 27, 6:05*pm, "James Hahn" <jh...@yahoo.co mwrote:
What do you mean by 'have to override the New()' - what is the message you
are getting? *Constructors aren't ordinary methods and are inherited
differently.

If the base class has no constructors or has a a paramaterless constructor
then you don't need a constructor. But if the base class does not have a
paramaterless constructor then your derived class must have a constructor
and the very first line of the constructor must be a call to the base class
constructor. Perhaps it was this requirement (not met in your sample code)
that made it appear that you were required to have a New(). It all depends
on what the base class's constructors are.

"Joe HM" <unixve...@yaho o.comwrote in message

news:e0******** *************** ***********@r66 g2000hsg.google groups.com...
Hello -
I have a question regarding overloading the New() in a MustInherit
Class. *The following show the code in question ...
MustInherit Class cAbstract
* *Public MustOverride Sub print()
* *Protected mName As String
* *Public Sub New() *' Why do I have to override this?
* * * *mName = "N/A"
* *End Sub
* *Public Sub New(ByVal aName As String)
* * * *mName = aName
* *End Sub
End Class
Class cChildOfAbstrac t
* *Inherits cAbstract
* *Public Overrides Sub print()
* * * *Console.WriteL ine("cChildOfAb stract.print() mName = '" &
mName & "'")
* *End Sub
End Class
...
Dim lInstanceOfChil d As New cChildOfAbstrac t("My Name") * ' Why is
this not working?
...
First of all, I have to override the New(). *Is this because I
overloaded it with the New(ByVal aName As String)?
Then I'm trying to call the overloaded constructor when I instantiate
the cChildOfAbstrac t but I cannot do that because apparently I have
too many arguments. *This works if I override the New(ByVal aName As
String) in the child class but why do I have to do that? *I thought I
would inherit that from the base class.
Thanks!
Joe- Hide quoted text -

- Show quoted text -
Oct 28 '08 #5
Hello -

I guess I just realized that I don't need an interface to the
overloaded New() with the parameter in the base class. The following
works ...

MustInherit Class cParent
....
End Class

Class cChild
Inherits cParent

Public Sub New(ByVal aArgument As String)
MyBase.New()
...
End Sub
End Class

....

Dim lDummy As cParent
lDummy = New cChild("")

Thanks,
Joe

On Oct 27, 6:59*pm, "Family Tree Mike"
<FamilyTreeM... @ThisOldHouse.c omwrote:
To the best of my knowledge, constructors are not inheritted in VB.Net. *Add
this constructor to your class cChildOfAbstrac t to accomplish what I think
you want:

*Public Sub New(ByVal s As String)
* MyBase.New(s)
*End Sub

I don't think you need the default constructor in your base class, at least
in what I was running.

"Joe HM" <unixve...@yaho o.comwrote in message

news:e0******** *************** ***********@r66 g2000hsg.google groups.com...
Hello -
I have a question regarding overloading the New() in a MustInherit
Class. *The following show the code in question ...
MustInherit Class cAbstract
* *Public MustOverride Sub print()
* *Protected mName As String
* *Public Sub New() *' Why do I have to override this?
* * * *mName = "N/A"
* *End Sub
* *Public Sub New(ByVal aName As String)
* * * *mName = aName
* *End Sub
End Class
Class cChildOfAbstrac t
* *Inherits cAbstract
* *Public Overrides Sub print()
* * * *Console.WriteL ine("cChildOfAb stract.print() mName = '" &
mName & "'")
* *End Sub
End Class
...
Dim lInstanceOfChil d As New cChildOfAbstrac t("My Name") * ' Why is
this not working?
...
First of all, I have to override the New(). *Is this because I
overloaded it with the New(ByVal aName As String)?
Then I'm trying to call the overloaded constructor when I instantiate
the cChildOfAbstrac t but I cannot do that because apparently I have
too many arguments. *This works if I override the New(ByVal aName As
String) in the child class but why do I have to do that? *I thought I
would inherit that from the base class.
Thanks!
Joe- Hide quoted text -

- Show quoted text -
Oct 28 '08 #6
On 2008-10-28, Joe HM <un*******@yaho o.comwrote:
Hello -

The following will cause a "Class 'cChild' must declare a 'Sub New'
because its base class 'cParent' does not have an accessible 'Sub New'
that can be called with no arguments."

MustInherit Class cParent
Public Sub New(ByVal aArgument As String)
MyBase.New()
End Sub
End Class

Class cChild
Inherits cParent
End Class

I thought that by default every class has a parameterless constructor
but I guess I was mistaken. Looks like I have to spell it out.

Thanks,
Joe
The compiler will only generate a default constructor if you have not
explicitly added one to the class. As soon as you add the constructor:

Public Sub New (ByVal aArgument As String)

The compiler assumes you have explicitly stated your desires, and no longer
generates a default constructor. This is how it works in pretty much all
statically compiled languages... And it makes sense, because a lot of times a
default no argument constructor doesn't make sense for a class.
--
Tom Shelton
Oct 28 '08 #7

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

Similar topics

5
6885
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: // TestA.h --------------------------------------- #include <iostream> enum Aval { FIRST_VALUE,
1
2225
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen pertaining to casting class A to class B, the implementation of the
1
1536
by: Lance | last post by:
I get an exception when attempting to open a form in design mode if the form inherits from a form that is declared as MustInherit. For example, if I have: Public MustInherit Class TestFormBase Inherits Windows.Forms.Form '...Normal Windows Form Designer generated code here Public MustOverride Function GetValue() As Integer End Class
4
2661
by: Francisco Amaro | last post by:
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)
3
2765
by: Philip Wagenaar | last post by:
I created a Mustinherit class with two must override readonly properties: Public MustInherit Class RequestPart Public MustOverride ReadOnly Property Description() As Boolean Public MustOverride ReadOnly Property Mandatory() As String End Class I the derived class I cannot override the property Mandatory because the return types differ:
6
1308
by: Joe HM | last post by:
Hello - The following code will crash if the TestA() instances are declared as Private and work just fine if they are Public. Is there a way to hide these instances to callers from outside the Module? It should only be possible to call the one instance of TestA() without the second argument. ModuleX
2
2195
by: Ray Cassick \(Home\) | last post by:
Since I got such good feedback from the Xml comments question I will ask this of the group as well. Is VS.NET 2005 handling the visual inheritance problems centered around controls and forms that are decorated with MustInherit any better than 2003 did? I am getting really tired of having to do the good old '#If DEBUG Then' workaround just to get by this.
5
2293
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
4
3254
by: Academia | last post by:
I create a form Public Class MyTool Inherits MyForm .... which uses:
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
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 we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.