473,699 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MustInherit Class with parameters in the constructor


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


Nov 20 '05 #1
4 2655
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(para mstring)
End Sub

"Francisco Amaro" <famaro.at.abac usemedia.dot.co m> wrote in message
news:uF******** ******@tk2msftn gp13.phx.gbl...

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


Nov 20 '05 #2
"Francisco Amaro" <famaro.at.abac usemedia.dot.co m> schrieb

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

Nov 20 '05 #3

"Marina" <so*****@nospam .com> wrote in message
news:O9******** ******@TK2MSFTN GP12.phx.gbl...
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(para mstring)
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
"Francisco Amaro" <famaro.at.abac usemedia.dot.co m> wrote in message
news:uF******** ******@tk2msftn gp13.phx.gbl...

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



Nov 20 '05 #4
* "Francisco Amaro" <famaro.at.abac usemedia.dot.co m> scripsit:
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("The Parameter")
End Sub
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5

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

Similar topics

2
21927
by: Vijay Singh | last post by:
How do you instantiat a class with a Class.forName( ) method, if the class's default constructor requires a parameter?
3
4618
by: Bob lazarchik | last post by:
I am trying to derive a class from an existing class with parameters in the constructor. Does anyone know why this is happening. The Code public class ClassA { public ClassA( int Param1 ) { }
5
8730
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but conceptually speaking : when do I define something as 'struct' and when as 'class' ? for example : if I want to represent a 'Time' thing, containing : - data members : hours, mins, secs
3
1330
by: Mike | last post by:
Hello In the following code,why are there 2 different Class1's.Is one a class definition and the other a function? namespace Music { public class Class1 { public Class1() {
5
4576
by: AlexVN | last post by:
Hi, I would like to know if someone investigated the method of creating classes dynamically depending on the name. I do not like a lot of ifs and I suppose that something like call_user_func_array('ClassName', array($param1, $param2, $param3, ..., $paramN)) should exist over the PHP language. Any suggestions? Thanks, Alexander
15
3843
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can* populate the variable, but the population is not *required* by the derived class (which must be the case). What would meet the requirements is if I create an abstract method in the base class that populates the member variable. In this case the...
61
2957
by: Sanders Kaufman | last post by:
I'm wondering if I'm doing this right, as far as using another class object as a PHP class property. class my_baseclass { var $Database; var $ErrorMessage; var $TableName; var $RecordSet; function my_baseclass(){ $this->TableName = "";
4
3252
by: Academia | last post by:
I create a form Public Class MyTool Inherits MyForm .... which uses:
6
4168
by: Joe HM | last post by:
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
0
8630
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
9199
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
9055
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8944
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8899
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
7786
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4391
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...

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.