473,698 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiple constructors - can they call each other?

public class myClass
{
public myClass(string param1)
{
//HOW DO I CALL myClass() below??
myClass(); // THIS DOES NOT WORK
}

public myClass()
{
//Do something here that ALL constructors should do. Hence, I want
to call it from
}
}

NOTE: I could just make a different method that the constructors call, but
I'd like to avoid this. Is there a way to do what I'm trying to do?
It also doesn't make sense to do "new myClass()" in the first constructor
because that just creates an entirely new instance of the class.

Thanks in advance!
Mark
Nov 15 '05 #1
2 22334
In article <en************ **@TK2MSFTNGP12 .phx.gbl>,
mf****@idonotli kespam.cce.umn. edu says...
public class myClass
{
public myClass(string param1)
{
//HOW DO I CALL myClass() below??
myClass(); // THIS DOES NOT WORK
}

public myClass()
{
//Do something here that ALL constructors should do. Hence, I want
to call it from
}
}

NOTE: I could just make a different method that the constructors call, but
I'd like to avoid this. Is there a way to do what I'm trying to do?
It also doesn't make sense to do "new myClass()" in the first constructor
because that just creates an entirely new instance of the class.

Thanks in advance!
Mark

You can do this using
public myClass(string param1) : this() <------------- here
{
//HOW DO I CALL myClass() below??
myClass(); // THIS DOES NOT WORK
}

Even when the other constustors take parameters you can pass them using
public myClass(string param1, int param2) : this(param2) .........

if you had a construcor like

public myClass(int param2)

--
Thanks
Mark mm
Nov 15 '05 #2

Hello Mark,

Yes in there own kind of way, you do something like this;

public class MyClass
{
public MyClass() //One of your constructors
{
//Something(s) you want this constructor to do fundamentally.. .
}

public MyClass(string param1) : this()
{
//Other things you want done but need to to or want to do other
things from the constructor above
}

public MyClass(int nAge, string param1) : this(param1)
{
//Get the picture???
}

} //class MyClass

Hope this helps you Mark go well.

SpotNet ;~?
"Mark" <mf****@idonotl ikespam.cce.umn .edu> wrote in message
news:en******** ******@TK2MSFTN GP12.phx.gbl...
public class myClass
{
public myClass(string param1)
{
//HOW DO I CALL myClass() below??
myClass(); // THIS DOES NOT WORK
}

public myClass()
{
//Do something here that ALL constructors should do. Hence, I want to call it from
}
}

NOTE: I could just make a different method that the constructors call, but
I'd like to avoid this. Is there a way to do what I'm trying to do?
It also doesn't make sense to do "new myClass()" in the first constructor
because that just creates an entirely new instance of the class.

Thanks in advance!
Mark

Nov 15 '05 #3

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

Similar topics

3
1982
by: John | last post by:
I would like to know how I can automatically call other scripts if I have to first display a message. The reason is I would like to display a message for about 30 seconds then automatically call another script but if I do this with header() I get an error. Another question I have is, Is there a PHP method equivalent to setInterval() in JavaScript? Thank you John
12
5310
by: Philip Smith | last post by:
Call this a C++ programmers hang-up if you like. I don't seem to be able to define multiple versions of __init__ in my matrix class (ie to initialise either from a list of values or from 2 dimensions (rows/columns)). Even if Python couldn't resolve the __init__ to use on the basis of argument types surely it could do so on the basis of argument numbers??? At any rate - any suggestions how I code this????
12
1825
by: Dave Rudolf | last post by:
Hi all, At the risk of mentioning Java in a C++ news group :), I'm curious if there is a way to reuse constructors in C++ similar to what can be done in Java. For instance, in java, I can do the following: public class Rectangle { public Rectangle( int width, int height ) {
2
3102
by: Neil Zanella | last post by:
Hello, AFAIK the only way to initialize a reference variable defined inside a class is to initialize it in an initializer list. However, when there are multiple constructors, this means that the initializer lists have to be cut and pasted from one constructor to another. This does not seem to lend itself particularly well to maintainablility. Calling a constructor from another in C++ is not legal unlike in Java. Also, functions other...
3
1540
by: Tony Dong | last post by:
Hi There I have two vb.net window projects made by vb.net, when I run one project and click a button in this project window form, how can I call other project window form ? Thanks Tony
0
1132
by: hastha23 | last post by:
Dear Friends, My oracle Version is 10g. I calling a function from sql select same time function body contain DML statement,that time is possible call function from sql? and again one, same function containing DML statement call other function or procedures is possible call other functions or procedures ?
0
8678
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
9166
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...
1
8899
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
7737
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
6525
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
5861
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
4371
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...
1
3052
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
3
2007
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.