TheSwamp

Code Red => .NET => Topic started by: jmaeding on September 23, 2008, 02:01:00 PM

Title: Sincpac code question
Post by: jmaeding on September 23, 2008, 02:01:00 PM
I was looking at the SincpacC3D code, and was not sure what this kind of statement does, from PlotStyleCommandParameter.cs:

public PlotStyleCommandParameter(string parameterPrefix) : this(parameterPrefix, true) { }

its clearly a constrcutor function, but what is the part after the : doing?
I can guess, but am sure I am wrong, its not an inheritance activity...
So am asking for raw C# help here, thx.
Title: Re: Sincpac code question
Post by: It's Alive! on September 23, 2008, 02:09:01 PM
It’s probably setting a default to another constructor in the class, i.e
PlotStyleCommandParameter(string parameterPrefix, bool flag)
Title: Re: Sincpac code question
Post by: It's Alive! on September 23, 2008, 02:36:31 PM
something like

Code: [Select]
  public class Foo
    {
        private string m_str; 
        private bool m_flag;

        public Foo(string str) : this(str, true){ }
        public Foo(string str, bool flag)
        {
            m_str = str;
            m_flag = flag;
        }
    }
Title: Re: Sincpac code question
Post by: Glenn R on September 23, 2008, 03:04:46 PM
Dan's right and it's sometimes called 'constructor chaining'.
Title: Re: Sincpac code question
Post by: Glenn R on September 23, 2008, 03:06:36 PM
In other words, you leverage other constructors, where it makes sense to do so, rather than supplying 'extra' constructors that overload and require an increasing number of parameters.
Title: Re: Sincpac code question
Post by: Spike Wilbury on September 23, 2008, 04:15:15 PM
That is done because in C# you can't have default arguments (am I right Glenn and Daniel? - if not I pay the next round of beers)

In C++ we can do something like this:

Code: [Select]
void MyFunction(double num, int index, CString str="My Default") { ... }
Title: Re: Sincpac code question
Post by: Glenn R on September 23, 2008, 04:18:14 PM
Spot on the money Luis...no defaulty argumentos for C#.
Title: Re: Sincpac code question
Post by: jmaeding on September 23, 2008, 04:19:15 PM
ah, makes sense now, the function below it has matching params, I had missed that.
Boy, what a useful thing, I can simplify several constructors I have done the hard way.
Title: Re: Sincpac code question
Post by: Glenn R on September 23, 2008, 04:29:40 PM
If that's new to you, I seriously suggest getting some books on C# and doing some reading...Andrew Troelsen's 'Pro C# blah blah' works from Apress spring to mind and are excellent.
Title: Re: Sincpac code question
Post by: jmaeding on September 23, 2008, 05:44:24 PM
I've got books that I know mention it, I had not noticed it for some reason.
Good advice though, I have a lot to learn.
Title: Re: Sincpac code question
Post by: It's Alive! on September 23, 2008, 06:00:22 PM
I have a lot to learn.

me too
Title: Re: Sincpac code question
Post by: Glenn R on September 23, 2008, 06:21:39 PM
me 3
Title: Re: Sincpac code question
Post by: Spike Wilbury on September 23, 2008, 06:25:57 PM
not me, I know to much already....   :-P     :lmao:



and now seriously:



me cuatro.
Title: Re: Sincpac code question
Post by: jmaeding on September 24, 2008, 04:29:57 PM
come on Sincovec, "Me Sinco"...
Title: Re: Sincpac code question
Post by: sinc on September 24, 2008, 04:35:58 PM
come on Sincovec, "Me Sinco"...

Septo you beat me to it...  Oucho these puns hurt.   :-)
Title: Re: Sincpac code question
Post by: sinc on September 24, 2008, 04:40:45 PM
Oops, skipped six.  It must've been taking a seis-ta.