473,410 Members | 1,857 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,410 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?????
Jul 21 '05 #1
1 1603
Antuane <An*****@testing.com.net> wrote:
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?
You've tried to call a constructor which doesn't exist. Constructors
aren't inherited. See the bottom of
http://www.pobox.com/~skeet/csharp/constructors.html for reasons why
this is a good thing.
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.
Yes.
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.


Just don't declare it then. A parameterless constructor is only
provided for you if you don't specify any other ones (I believe, anyway
- that's certainly true in C#, and from experimentation it appears to
be true in VB.NET too.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

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...
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...
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 ----------------------------------------...
2
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
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: 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
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
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,...
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...
0
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
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
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...
0
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...

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.