473,325 Members | 2,828 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,325 software developers and data experts.

constructor

hi,

how can I call a constructor in a constructor?

here is my sample

public class Test
{
public int _Size = 0;
public string _Name;

Test(string name)
{
// call of constructor Test(name, 100);
}

Test(string name, int size)
{
_Name = name;
_Size = size;
// do something....
}
}
Test tvar = new Test("testman");

// tvar._Size should now be 100...

Could you help me please?

Thanks
Alex
Nov 19 '05 #1
2 927
Alexander Widera wrote:
hi,

how can I call a constructor in a constructor?

here is my sample

public class Test
{
public int _Size = 0;
public string _Name;

Test(string name)
{
// call of constructor Test(name, 100);
}

Test(string name, int size)
{
_Name = name;
_Size = size;
// do something....
}
}
Test tvar = new Test("testman");

// tvar._Size should now be 100...

Could you help me please?

Thanks
Alex


Test(string name): this(name, 100)
{
}

The code in the other constructor will be called BEFORE the code
in this constructor is executed

Hans Kesting
Nov 19 '05 #2
Try sth like this:

public class Test
{
public int _Size = 0;
public string _Name;

Test(string name):this(name, 100)
{
}

Test(string name, int size)
{
_Name = name;
_Size = size;
// do something....
}
}
http://msdn.microsoft.com/library/en...ec_10_10_6.asp
--
C# Dev
"Alexander Widera" wrote:
hi,

how can I call a constructor in a constructor?

here is my sample

public class Test
{
public int _Size = 0;
public string _Name;

Test(string name)
{
// call of constructor Test(name, 100);
}

Test(string name, int size)
{
_Name = name;
_Size = size;
// do something....
}
}
Test tvar = new Test("testman");

// tvar._Size should now be 100...

Could you help me please?

Thanks
Alex

Nov 19 '05 #3

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) {
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
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) .
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
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...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
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...
13
by: sam_cit | last post by:
Hi Everyone, I have the following unit to explain the problem that i have, class sample { public : sample() { printf("in sample...\n"); }
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: 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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.