PC Review


Reply
Thread Tools Rate Thread

can I call from one constructor another constructor ?

 
 
Paul
Guest
Posts: n/a
 
      30th Jun 2004
public class A
{
public A ()
{
// here I would like to call the second version of _ctor, how to
accomplish this ?
}

public A (int a, int b, int c)
{
// some code
}
}

Thanks for any advice,
Paul


 
Reply With Quote
 
 
 
 
AlexS
Guest
Posts: n/a
 
      30th Jun 2004
Paul.

use this to reference current instance.

E.g.

public A() : this(1,2,3) {
....
}

HTH
Alex

"Paul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> public class A
> {
> public A ()
> {
> // here I would like to call the second version of _ctor, how to
> accomplish this ?
> }
>
> public A (int a, int b, int c)
> {
> // some code
> }
> }
>
> Thanks for any advice,
> Paul
>
>



 
Reply With Quote
 
John Wood
Guest
Posts: n/a
 
      30th Jun 2004
You cannot. You will have to move the code in the second constructor into a
function, called 'Initialize' for example, and then invoke that function
from both constructors.

You can, however, invoke inherited contructors in the constructor signature
using the base keyword, eg:

public A (int param1, string param2) : base(param1, param2)
{
...
}

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Paul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> public class A
> {
> public A ()
> {
> // here I would like to call the second version of _ctor, how to
> accomplish this ?
> }
>
> public A (int a, int b, int c)
> {
> // some code
> }
> }
>
> Thanks for any advice,
> Paul
>
>



 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      30th Jun 2004
do it like this:
public A() : this(1, 2, 3)
{
// other constructor will be called before anything
// happens in this one.
}

Chris

"Paul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> public class A
> {
> public A ()
> {
> // here I would like to call the second version of _ctor, how to
> accomplish this ?
> }
>
> public A (int a, int b, int c)
> {
> // some code
> }
> }
>
> Thanks for any advice,
> Paul
>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      30th Jun 2004
Paul <(E-Mail Removed)> wrote:
> public class A
> {
> public A ()
> {
> // here I would like to call the second version of _ctor, how to
> accomplish this ?
> }
>
> public A (int a, int b, int c)
> {
> // some code
> }
> }


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

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      30th Jun 2004
John Wood <(E-Mail Removed)> wrote:
> You cannot. You will have to move the code in the second constructor into a
> function, called 'Initialize' for example, and then invoke that function
> from both constructors.


Yes you can - just as your code below, but using "this" instead of
"base".

> You can, however, invoke inherited contructors in the constructor signature
> using the base keyword, eg:
>
> public A (int param1, string param2) : base(param1, param2)
> {
> ...
> }


Point of pedantry: constructors aren't inherited. That invokes the base
constructor.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
John Wood
Guest
Posts: n/a
 
      30th Jun 2004
> Yes you can - just as your code below, but using "this" instead of
> "base".

Yeah true you can (I admit I forgot), but I rarely use that because you have
little control over when the referenced constructor is called (it always
runs before the constructor body).


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      30th Jun 2004
John Wood <(E-Mail Removed)> wrote:
> > Yes you can - just as your code below, but using "this" instead of
> > "base".

> Yeah true you can (I admit I forgot), but I rarely use that because you have
> little control over when the referenced constructor is called (it always
> runs before the constructor body).


That doesn't mean it's not useful though. I use it all the time when
providing constructs which effectively default various values - each
constructor calls a version with more parameters, either directly going
to the "full" one or going in stages. I believe this is fairly common.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
John Wood
Guest
Posts: n/a
 
      30th Jun 2004
I still don't get why they didn't add optional parameters in C#...
Constructor overloads can get pretty messy, given all the permutations you
have to provide.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> John Wood <(E-Mail Removed)> wrote:
> > > Yes you can - just as your code below, but using "this" instead of
> > > "base".

> > Yeah true you can (I admit I forgot), but I rarely use that because you

have
> > little control over when the referenced constructor is called (it always
> > runs before the constructor body).

>
> That doesn't mean it's not useful though. I use it all the time when
> providing constructs which effectively default various values - each
> constructor calls a version with more parameters, either directly going
> to the "full" one or going in stages. I believe this is fairly common.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
John Wood
Guest
Posts: n/a
 
      30th Jun 2004
Incidentally, I typically use the essence pattern for constructing complex
objects now.. saves having all those overloads cluttering the class.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> John Wood <(E-Mail Removed)> wrote:
> > > Yes you can - just as your code below, but using "this" instead of
> > > "base".

> > Yeah true you can (I admit I forgot), but I rarely use that because you

have
> > little control over when the referenced constructor is called (it always
> > runs before the constructor body).

>
> That doesn't mean it's not useful though. I use it all the time when
> providing constructs which effectively default various values - each
> constructor calls a version with more parameters, either directly going
> to the "full" one or going in stages. I believe this is fairly common.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I get a constructor to call another constructor tshad Microsoft C# .NET 6 20th Jan 2009 01:00 AM
Constructor call constructor Geoffrey Microsoft C# .NET 7 4th Apr 2006 05:47 PM
constructor call another constructor Le, Thanh-Nhan Microsoft C# .NET 3 31st Dec 2004 08:12 AM
Constructor call within a constructor Greg Microsoft C# .NET 4 9th Sep 2004 01:53 PM
can I call from one constructor another constructor ? Paul Microsoft C# .NET 22 14th Jul 2004 02:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:25 PM.