constructor chaining vs calling this. vs calling named constructor?

  • Thread starter Thread starter Flip
  • Start date Start date
F

Flip

I'm half way through watching a cool video from
MS(http://msdn.microsoft.com/netframework/programming/classlibraries/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?
 
Flip said:
I'm half way through watching a cool video from
MS(http://msdn.microsoft.com/netframework/programming/classlibraries/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?
 
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");
}
}
}
 
Flip said:
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
:)

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...
 
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.
 
Flip said:
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
:)

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...
 
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).
 
Bruce Wood said:
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.
 
Oh, I'd do it piecemeal: CTL-A, CTL-K, CTL-F whenever I noticed some
"non-standard" code.

I'm a pragmatist. :)
 
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.
 
Back
Top