Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Cascading through the constructors

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
K Viltersten - 15 Mar 2008 21:33 GMT
As a human, i'm lazy, so if i need to
write a class with three constructors,
i'd like to minimize the work. I'm used
to two different approaches and i'd
like to get an opinion on which is to
be most preferred.

// 1. Constructors calling each other.
class C {
C () {this (5);}
C (int i) {this (5, 3);}
C (int i, int j) {} }

// 2. Constructors calling a common method.
class C {
C () {common (5, 3);}
C (int i) {common (i, 3);}
C (int i, int j) {common (i, j);}
void common (int i, int j) {} }

My applogies for the ugly code. I tried to
keep it short.

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 15 Mar 2008 21:43 GMT
> As a human, i'm lazy, so if i need to
> write a class with three constructors,
[quoted text clipped - 18 lines]
> My applogies for the ugly code. I tried to
> keep it short.

I personally usually use option 1 - although I vary between a "long"
constructor chain (where the parameterless calls the single parameter,
which calls the double parameter) and "short" constructor chains (where
every constructor calls the most complicated one).

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

K Viltersten - 15 Mar 2008 22:39 GMT
>> Which is to be most preferred?
>>
[quoted text clipped - 20 lines]
> and "short" constructor chains (where every
> constructor calls the most complicated one).

Thanks!

Just one more thing. I don't seem to be able
to compile

 C () {this (5, 3);}

as the compiler nags about "method name
expected". I expected the same syntax as in
Java, perhaps foolishly... Help?

I suspect i could use the syntax:

 C () : this (5, 3) {}

but i don't want to risk it. I want to make
sure that the LAST thing done in the simple
constructor is calling the complicated one,
not the first thing.

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 15 Mar 2008 22:52 GMT
> Thanks!
>
[quoted text clipped - 6 lines]
> expected". I expected the same syntax as in
> Java, perhaps foolishly... Help?

You've got it here:

> I suspect i could use the syntax:
>
[quoted text clipped - 4 lines]
> constructor is calling the complicated one,
> not the first thing.

No, it's just like Java in that respect (though not in the syntax) -
the chained constructor call is always the first thing to be executed.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

K Viltersten - 15 Mar 2008 23:02 GMT
>> Just one more thing. I don't seem to be able
>> to compile
[quoted text clipped - 8 lines]
>
>>   C () : this (5, 3) {}

Are you saying that i can't call a constructor
in a class C from INSIDE an other constructor
in the same class C?! That's surprising...

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 15 Mar 2008 23:36 GMT
> > You've got it here:
> >
[quoted text clipped - 3 lines]
> in a class C from INSIDE an other constructor
> in the same class C?! That's surprising...

Well, bear in mind that every constructor is meant to *eventually* call
a base class constructor (potentially though chaining) - and that is
designed to happen before any other code in your class is called (aside
from variable initializers).

As I say, the same restriction is in Java. I don't think the CLR
requires it, admittedly.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

K Viltersten - 16 Mar 2008 00:55 GMT
>> Are you saying that i can't call a constructor
>> in a class C from INSIDE an other constructor
[quoted text clipped - 8 lines]
> As I say, the same restriction is in Java. I don't
> think the CLR requires it, admittedly.

It's been a while i've been working with Java but i
remember that the construction was as follows.

class J {
public J () {this(5);}
public J (int i) {} }

I.e., the call to a fellow constructor has been
placed INSIDE the body and one could, as far my
memory serves me, execute statements BEFORE the
call to the constructor.

The call to the base, however, wasn't so freely
placeable. It had to come first in the constructor.

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Arne Vajhøj - 16 Mar 2008 01:32 GMT
> It's been a while i've been working with Java but i
> remember that the construction was as follows.
[quoted text clipped - 7 lines]
> memory serves me, execute statements BEFORE the
> call to the constructor.

The first is correct. You call this inside {}.

The second is not correct. You can not call
other statements before this.

Arne
K Viltersten - 16 Mar 2008 09:16 GMT
>> It's been a while i've been working with Java but i
>> remember that the construction was as follows.
[quoted text clipped - 12 lines]
> The second is not correct. You can not call
> other statements before this.

Then i guess i was mistaken. It's been a while
since i've touched anything bean-related, so i'll
stand corrected.

In that case, thank you both for the correction.

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 16 Mar 2008 08:37 GMT
> It's been a while i've been working with Java but i
> remember that the construction was as follows.
[quoted text clipped - 5 lines]
> I.e., the call to a fellow constructor has been
> placed INSIDE the body

Correct.

> and one could, as far my
> memory serves me, execute statements BEFORE the
> call to the constructor.

Incorrect. From the spec
(http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html)

<quote>
The first statement of a constructor body may be an explicit invocation
of another constructor of the same class or of the direct superclass (§
8.8.7.1).

   ConstructorBody:
       { ExplicitConstructorInvocationopt BlockStatementsopt }

</quote>

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.