473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Problems with the contructor/new() method in the subclass

I've got a class as follows,

PUBLIC CLASS BaseClass

Sub New()
End Sub

Sub New(byval cName as String)
End Sub

Sub New(byval cName as String, nAge as Int16)
End Sub

END CLASS

PUBLIC CLASS Person
Inherits BaseClass
END CLASS
** Coding
If suppose i try the following piece of coding,
Dim oPerson as New Person("Jack", "Daniels")

"Then i get an error saying that the NEW method cannot accecpt so may
parameters

1. What have i done wrong?
2. I want to define such base NEW() - constructors, & do'nt want to write
these coding out in the subclasses.
But it seems taht i have to do so.

2. Is is possible for me to restrict the Sub NEW() contructor - the one
wihout parameters.
i.e. what i want to do is, when initializing the Person class, i want to
make it impossible to initialize this class by using the New() constructor
without any paramters.
Right now, what i do is,

PUBLIC CLass Person
Sub New()
THROW New Exception("Try using the Other Constructors which
accept parameters")
End Sub
.....

Is there another way of achieving the above?????
Nov 21 '05 #1
2 958
"Antuane" <An*****@testing.com.net> schrieb:
I've got a class as follows,

PUBLIC CLASS BaseClass

Sub New()
End Sub

Sub New(byval cName as String)
End Sub

Sub New(byval cName as String, nAge as Int16)
End Sub

END CLASS

PUBLIC CLASS Person
Inherits BaseClass
END CLASS
** Coding
If suppose i try the following piece of coding,
Dim oPerson as New Person("Jack", "Daniels")

"Then i get an error saying that the NEW method cannot accecpt so may
parameters

1. What have i done wrong?
2. I want to define such base NEW() - constructors, & do'nt want to write
these coding out in the subclasses.
But it seems taht i have to do so.
Constructors defined in the base class are not inherited automatically. You
will have to add them to the base class too:

\\\
..
..
..
Sub New(byval cName as String, nAge as Int16)
MyBase.New(cName, nAge)
End Sub
..
..
..
///
2. Is is possible for me to restrict the Sub NEW() contructor - the one
wihout parameters.
i.e. what i want to do is, when initializing the Person class, i want
to
make it impossible to initialize this class by using the New() constructor
without any paramters.
Right now, what i do is,

PUBLIC CLass Person
Sub New()
THROW New Exception("Try using the Other Constructors which
accept parameters")
End Sub


Why do you add a parameterless ctor at all (assuming you add parameterized
ctors too)?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2
Comments inline....

"Antuane" <An*****@testing.com.net> wrote

<snipped for brievity>
** Coding
If suppose i try the following piece of coding,
Dim oPerson as New Person("Jack", "Daniels")

"Then i get an error saying that the NEW method cannot accecpt so may
parameters

1. What have i done wrong?
You did not supply New routines in your derived class. It is a class itself,
and it needs constructors just like your base class.

2. I want to define such base NEW() - constructors, & do'nt want to write
these coding out in the subclasses.
Then try Copy and Paste? Really, you have to have them out there if you
want that class to accept parameters during construction.
But it seems taht i have to do so.
Correct.


2. Is is possible for me to restrict the Sub NEW() contructor - the one
wihout parameters.
Yes.
Is there another way of achieving the above?????
Make that constructor private:
Public Class Person
PRIVATE Sub New()
' Being private, only code in this class can use this routine.
End Sub

Have fun!
LFS
Nov 21 '05 #3

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

Similar topics

31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
2
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for...
1
by: Antuane | last post by:
I've got a class as follows, PUBLIC CLASS BaseClass Sub New() End Sub Sub New(byval cName as String) End Sub
2
by: psane | last post by:
I have the following code #include <iostream> #include <string> using namespace std; class superclass { public: superclass () {}
1
by: ziga.seilnacht | last post by:
""" I am trying to write some classes representing the quaternion number. I wrote a base class, which implements only the numerical interface, and a few subclasses, which provide methods for their...
3
by: john | last post by:
I have a situation where i have a base class and a sub-class. A null instance of the sub-class is passed into a function. The function needs to create a new instance of the sub-class, but the...
4
by: Jazper Manto | last post by:
hi what is the difference between virtual / override and new? until now i only knew the virtual / override thing. thanx for hint. Jazper //--- new ----------------------------------------...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
1
by: Bucs44 | last post by:
I'm new to programming and I'm trying to understand something. I have to write an additional subclass from the code below. Attributes I need to use are for a Rowboat, specifically type of material...
4
by: Kurt Smith | last post by:
Hi List: Class inheritance noob here. For context, I have the following base class and subclass: class Base(object): def __init__(self, val): self.val = val
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.