473,466 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Constructor calling a constructor

How do you go about calling a constructor in the same class from another
constructor? (see below)

- Roger

ie.

public class1()
{
Does some general initialization
}

public class1(string Thing1)
{
/// Call class1() for general initialization
/// set a property to Thing1
}
Nov 16 '05 #1
4 1475
public class1(string Thing1) : this() {
// set a property to Thing1
}

"Roger Webb" <rw***@nos.twia.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
How do you go about calling a constructor in the same class from another
constructor? (see below)

- Roger

ie.

public class1()
{
Does some general initialization
}

public class1(string Thing1)
{
/// Call class1() for general initialization
/// set a property to Thing1
}

Nov 16 '05 #2
Eco

Hi,

This is how you do it:

public class1
{
public class1()
{
// Initialization code
}

public class1(string thing1) : this()
{
this._thing1 = thing1;
}

//...

}

"Roger Webb" <rw***@nos.twia.org> escribió en el mensaje
news:%2****************@tk2msftngp13.phx.gbl...
How do you go about calling a constructor in the same class from another
constructor? (see below)

- Roger

ie.

public class1()
{
Does some general initialization
}

public class1(string Thing1)
{
/// Call class1() for general initialization
/// set a property to Thing1
}

Nov 16 '05 #3
"Roger Webb" <rw***@nos.twia.org> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
How do you go about calling a constructor in the same class from another
constructor? (see below)

- Roger

ie.

public class1()
{
Does some general initialization
}

public class1(string Thing1)
{
/// Call class1() for general initialization
/// set a property to Thing1
}


I believe you could do something like so:

public class1(string Thing1) : this ()
{
/// set properties
}

This is very useful for designing overloaded types.

- carl
Nov 16 '05 #4
"Roger Webb" <rw***@nos.twia.org> wrote:
How do you go about calling a constructor in the
same class from another constructor?


Use the 'this' keyword (or 'base', to call the constructor of a base
class):

public Class1() { /* ... */ }
public Class1(string s) : this() { /* ... */ }

P.
Nov 16 '05 #5

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

Similar topics

3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
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) .
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
5
by: ffrugone | last post by:
My scenario involves two classes and a database. I have the classes "Broom" and "Closet". I want to use a static method from the "Closet" class to search the database for a matching "Broom". If...
3
by: Igor.Smirnov | last post by:
Hi, Why g++ in Linux does not call copy constructor at return statement? I have detected this at Sicentific Linux 4.1 (g++ v. 3.4.3) and Red Hat Linux 8.0 (g++ v. 3.2). Microsoft Visual...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
1
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
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
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.