PC Review


Reply
Thread Tools Rate Thread

constructor chaining vs calling this. vs calling named constructor?

 
 
Flip
Guest
Posts: n/a
 
      23rd Mar 2005
I'm half way through watching a cool video from
MS(http://msdn.microsoft.com/netframewo...s/membertypes/).

One thing that came out which confuses me is the use of calling the named
constructors vs constructor chaining. Why is one of the constructors
calling another construtor by name instead of using the constructor
chaining?

One immediate advantage I saw was the use of intellisense in VisualStudio,
but that's an IDE issue, not a language issue and should not be considered
(IMHO).

Any other ideas? Please?


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Mar 2005
Flip <[remove_me](E-Mail Removed)> wrote:
> I'm half way through watching a cool video from
> MS(http://msdn.microsoft.com/netframewo...s/membertypes/).
>
> One thing that came out which confuses me is the use of calling the named
> constructors vs constructor chaining. Why is one of the constructors
> calling another construtor by name instead of using the constructor
> chaining?


Could you post the sample code? I'd rather not download and then have
to watch a long video in order to answer the question. Which language
are you talking about, btw?

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Flip
Guest
Posts: n/a
 
      23rd Mar 2005
> Could you post the sample code? I'd rather not download and then have
Unfortunately I don't use C# at work, but I'll do my best to whip something
up off the top of my head (ie. please disregard any compilation issues :<).
I WISH I was doing C# stuff during the day, but I'm working on that! :> haha

> are you talking about, btw?

If you haven't guessed, C#. :> haha

I think I just realized what it was. :< The example below is using the
Class name Foo, but the methods Bar I thougth (:<) were the constructors are
just regular methods. :< Sorry for the confusion. The example I was
thinking of was the Testing() constructor that takes two strings and wanting
to call another constructor. I was able to get it to work actually (figured
out how to get TextPad to edit and compile C# :>).

Thanks.

PS. I noticed on the video, the guy's code from MS has the opening block
braces on the same line of code as the block it's opening. I was told it's
better C/C++ and by extension C# format to put them on the next line. Which
is correct/most widely accepted out there?

--------------------code snippet starge here---------------------
using System;

namespace ConstructorTesting
{
//here's the example from the video
public class Foo{
private const string defaultForA = "a default";
private const int defaultForB = 42;

public Foo()
{
Console.WriteLine("Foo()");
}
public void Bar()
{
Bar(defaultForA, defaultForB);
}
public void Bar(string a)
{
Bar(a, defaultForB);
}
public virtual void Bar(string a, int b)
{
Console.WriteLine( "string a = " + a + ", int b = " + b);
}

}
public class Test
{
public Test()
{
Console.WriteLine("empty constructor");
}

public Test(string oneStringArgument)
{
Console.WriteLine("one string argument constructor = " +
oneStringArgument);
}

public Test(int oneIntArgument)
{
Console.WriteLine("one int argument constructor = " + oneIntArgument);
}

public Test(string oneStringArgument, string twoStringArgument):this("two
string arguments")
{
Console.WriteLine( "two string arguments");
}

static void Main(string[] args)
{
Console.WriteLine("beg");

Test test1 = new Test();

Test test2 = new Test("OneArgument");

Test test3 = new Test(1);

Test test4 = new Test("now", "later");

Foo foo1 = new Foo();
foo1.Bar();
foo1.Bar("Testing");
foo1.Bar("TestingAgain", 4);
Console.WriteLine("fin");
}
}
}




 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Mar 2005
Flip <[remove_me](E-Mail Removed)> wrote:
> > Could you post the sample code? I'd rather not download and then have

> Unfortunately I don't use C# at work, but I'll do my best to whip something
> up off the top of my head (ie. please disregard any compilation issues :<).
> I WISH I was doing C# stuff during the day, but I'm working on that! :> haha




> > are you talking about, btw?

> If you haven't guessed, C#. :> haha


Oops, sorry - thought this was in the .framework group. Slip of the
eyes, or whatever

> I think I just realized what it was. :< The example below is using the
> Class name Foo, but the methods Bar I thougth (:<) were the constructors are
> just regular methods. :< Sorry for the confusion. The example I was
> thinking of was the Testing() constructor that takes two strings and wanting
> to call another constructor. I was able to get it to work actually (figured
> out how to get TextPad to edit and compile C# :>).


Ah, right, I see.

> PS. I noticed on the video, the guy's code from MS has the opening block
> braces on the same line of code as the block it's opening. I was told it's
> better C/C++ and by extension C# format to put them on the next line. Which
> is correct/most widely accepted out there?


I think these days the "next line" style is more common, but I wouldn't
like to say for sure. Personally I find the "end of the same line"
convention horribly unreadable, but there we go...

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Flip
Guest
Posts: n/a
 
      23rd Mar 2005
> Oops, sorry - thought this was in the .framework group. Slip of the
Oh sorry about that, I didn't mean to indicate the newsgroup. More my
previous text had C# in it a few times. :> But ya, I just realized the
newsgroup name too! :> haha

> I think these days the "next line" style is more common, but I wouldn't
> like to say for sure. Personally I find the "end of the same line"
> convention horribly unreadable, but there we go...

You find it hard to read? Interesting. We got into that heated debate at
my work doing java code. A friend of mine doing C++ work told me the reason
the "same line" was gaining popularity was cause of poeple (for good for for
bad, like me) who were copying the style from the books they were reading,
which would put the braces on the same line to save space in the book for
their code.

So the next line convention is indeed the way to go then? I figure if I'm
doing to move to C#, I might as well do it properly! :>

Thanks again.


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Mar 2005
Flip <[remove_me](E-Mail Removed)> wrote:
> > Oops, sorry - thought this was in the .framework group. Slip of the

> Oh sorry about that, I didn't mean to indicate the newsgroup. More my
> previous text had C# in it a few times. :> But ya, I just realized the
> newsgroup name too! :> haha




> > I think these days the "next line" style is more common, but I wouldn't
> > like to say for sure. Personally I find the "end of the same line"
> > convention horribly unreadable, but there we go...

> You find it hard to read? Interesting. We got into that heated debate at
> my work doing java code. A friend of mine doing C++ work told me the reason
> the "same line" was gaining popularity was cause of poeple (for good for for
> bad, like me) who were copying the style from the books they were reading,
> which would put the braces on the same line to save space in the book for
> their code.


Yes - that's nothing new, of course. My feeling is that what's good for
a publisher isn't always good for real code

> So the next line convention is indeed the way to go then? I figure if I'm
> doing to move to C#, I might as well do it properly! :>


It's certainly the way I code, and having worked on code with both
conventions, I find it much clearer. But then, as I use it habitually,
I'm biased...

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      24th Mar 2005
My rule is: do whatever the IDE does... don't fight the product.

Since Visual Studio puts the brace on the next line by default, I leave
it there. I wonder about people who spend the time to move that little
brace up to the previous line. If VS were to start putting braces
on the same line, I would leave them there, too.

This goes back to my #1 rule of programming: a mediocre standard is
better than no standard at all. Or, phrased another way, a "better way"
is de facto _not_ better... precisely because it's different from what
everyone else is doing (unless, of course, it is orders of magnitude
better... that's a whole other can of worms).

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      24th Mar 2005
Bruce Wood <(E-Mail Removed)> wrote:
> My rule is: do whatever the IDE does... don't fight the product.
>
> Since Visual Studio puts the brace on the next line by default, I leave
> it there. I wonder about people who spend the time to move that little
> brace up to the previous line. If VS were to start putting braces
> on the same line, I would leave them there, too.


It's very simple to change the default though - it's under Options,
Text Editor, C#, Formatting.

> This goes back to my #1 rule of programming: a mediocre standard is
> better than no standard at all. Or, phrased another way, a "better way"
> is de facto _not_ better... precisely because it's different from what
> everyone else is doing (unless, of course, it is orders of magnitude
> better... that's a whole other can of worms).


But given that rule, if the default in VS.NET now *changed* to be "end
of line", you'd have to either change all your old code, or go against
the default for new code.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      28th Mar 2005
Oh, I'd do it piecemeal: CTL-A, CTL-K, CTL-F whenever I noticed some
"non-standard" code.

I'm a pragmatist.

 
Reply With Quote
 
Flip
Guest
Posts: n/a
 
      29th Mar 2005
> Oh, I'd do it piecemeal: CTL-A, CTL-K, CTL-F whenever I noticed some
> "non-standard" code.

I also do that! I just wish there were a quicker standard keystroke for
automatically formatting instead of having to select everything. Something
a bit more intelligent, like if the CTRL+K, CTRL+F recognizes nothing is
selected, then assume everything is the targe/selection and format it all.
Just a thought.


 
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
Calling a constructor from another constructor Tom P. Microsoft C# .NET 1 25th Jul 2008 09:44 PM
Calling another constructor from constructor dodger_web Microsoft C# .NET 2 18th Aug 2006 01:49 PM
Calling a struct constructor in a class constructor body Karl M Microsoft VC .NET 4 19th Dec 2004 01:21 PM
Constructor calling a constructor Roger Webb Microsoft C# .NET 4 8th Sep 2004 11:31 PM
Calling overloaded constructor with values inside another constructor Tristan Microsoft C# .NET 2 30th Mar 2004 01:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:50 PM.