471,120 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,120 software developers and data experts.

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 cChildOfAbstract
Inherits cAbstract

Public Overrides Sub print()
Console.WriteLine("cChildOfAbstract.print() mName = '" &
mName & "'")
End Sub
End Class

....
Dim lInstanceOfChild As New cChildOfAbstract("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 cChildOfAbstract 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 4037
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*******@yahoo.comwrote in message
news:e0**********************************@r66g2000 hsg.googlegroups.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 cChildOfAbstract
Inherits cAbstract

Public Overrides Sub print()
Console.WriteLine("cChildOfAbstract.print() mName = '" &
mName & "'")
End Sub
End Class

...
Dim lInstanceOfChild As New cChildOfAbstract("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 cChildOfAbstract 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 cChildOfAbstract 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*******@yahoo.comwrote in message
news:e0**********************************@r66g2000 hsg.googlegroups.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 cChildOfAbstract
Inherits cAbstract

Public Overrides Sub print()
Console.WriteLine("cChildOfAbstract.print() mName = '" &
mName & "'")
End Sub
End Class

...
Dim lInstanceOfChild As New cChildOfAbstract("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 cChildOfAbstract 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...@yahoo.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 cChildOfAbstract
Inherits cAbstract

Public Overrides Sub print()
Console.WriteLine("cChildOfAbstract.print() mName = '" &
mName & "'")
End Sub
End Class

...
Dim lInstanceOfChild As New cChildOfAbstract("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 cChildOfAbstract 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.comwrote:
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...@yahoo.comwrote in message

news:e0**********************************@r66g2000 hsg.googlegroups.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 cChildOfAbstract
* *Inherits cAbstract
* *Public Overrides Sub print()
* * * *Console.WriteLine("cChildOfAbstract.print() mName = '" &
mName & "'")
* *End Sub
End Class
...
Dim lInstanceOfChild As New cChildOfAbstract("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 cChildOfAbstract 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.comwrote:
To the best of my knowledge, constructors are not inheritted in VB.Net. *Add
this constructor to your class cChildOfAbstract 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...@yahoo.comwrote in message

news:e0**********************************@r66g2000 hsg.googlegroups.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 cChildOfAbstract
* *Inherits cAbstract
* *Public Overrides Sub print()
* * * *Console.WriteLine("cChildOfAbstract.print() mName = '" &
mName & "'")
* *End Sub
End Class
...
Dim lInstanceOfChild As New cChildOfAbstract("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 cChildOfAbstract 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*******@yahoo.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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andy Jarrell | last post: by
1 post views Thread by masood.iqbal | last post: by
1 post views Thread by Lance | last post: by
2 posts views Thread by Ray Cassick \(Home\) | last post: by
4 posts views Thread by Academia | last post: by

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.