473,322 Members | 1,480 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,322 software developers and data experts.

Can I call another constructor from a constructor?

Tom
Is it possible to do something like this:

public class CommentDlg : System.Windows.Forms.Form
{

[...]

public CommentDlg()
{
InitializeComponent();
}

public CommentDlg(string comment)
{
CommentDlg(); // <<<--- this generates a compile error
Comment = comment;
}

[...]

}
Nov 16 '05 #1
3 28501
public class Test
{
public Test()
{
Console.WriteLine("Test()");
}
public Test(string text) : this()
{
Console.WriteLine("Test(string text)");
}
}

There are cases when you may want a private method that contains shared
logic among many contructors. Then just call that from each constructor.
If it throws and exception, so does the constructor (if you don't catch it.)

--
William Stacey, MVP

"Tom" <nf*@nospam.com> wrote in message
news:Oh**************@TK2MSFTNGP11.phx.gbl...
Is it possible to do something like this:

public class CommentDlg : System.Windows.Forms.Form
{

[...]

public CommentDlg()
{
InitializeComponent();
}

public CommentDlg(string comment)
{
CommentDlg(); // <<<--- this generates a compile error
Comment = comment;
}

[...]

}


Nov 16 '05 #2
Tom <nf*@nospam.com> wrote:
Is it possible to do something like this:


<snip>

See http://www.pobox.com/~skeet/csharp/constructors.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Hi Tom!
You can do it like this:

public CommentDlg()
{
// this is my default constructor
}
public CommentDlg(string comment) : this ()
{
// some code here
}

when you call CommentDlg(string comment) constructor, CommentDlg() will
execute first and then CommentDlg(string comment)...

ofcource, you can call other constructor too, like this:

public CommentDlg() {}
public CommentDlg(string comment) : this () {}
public CommentDlg(string someData, int hello) : this (someData) {}

if you call public CommentDlg(string someData, int hello), execution order
is:
1. public CommentDlg() {}
2. public CommentDlg(string comment) : this () {}
3. public CommentDlg(string someData, int hello) : this (someData) {}
hope this helps...
Jeti

public class CommentDlg : System.Windows.Forms.Form
{

[...]

public CommentDlg()
{
InitializeComponent();
}

public CommentDlg(string comment)
{
CommentDlg(); // <<<--- this generates a compile error
Comment = comment;
}

[...]

}

Nov 16 '05 #4

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

Similar topics

3
by: S³awek | last post by:
Can one constructor of an object call another constructor of the same class? class foo { foo(float f, int i) // a "full" constructor { ... } foo(int i) // a "simplified" constructor {
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
4
by: Greg | last post by:
Is it possible to call a constructor from within a constructor I mean Class A { public A(string getModifiedVal) { .........
18
by: AlexanderVX | last post by:
How do I write a constructor mehtod call in this case /*-----------*/ template<typename Tclass CObjectPoolImpl { public: void smth(T* pObj) { if (pObj)
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
7
by: cppquester | last post by:
What does this code do? #include <iostream> class A { public: A() { std::cout << "A::A()" << std::endl;} };
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
15
by: asm23 | last post by:
Hi, everyone, I'm studying the <<Thinking in C++>volume Two. In Chapter One, the example code : Auto_ptr.cpp //------------------------------------------------------- #include <memory> #include...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.