Author Topic: Sincpac code question  (Read 5595 times)

0 Members and 1 Guest are viewing this topic.

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Sincpac code question
« 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.
James Maeding

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8788
  • AKA Daniel
Re: Sincpac code question
« Reply #1 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)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8788
  • AKA Daniel
Re: Sincpac code question
« Reply #2 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;
        }
    }

Glenn R

  • Guest
Re: Sincpac code question
« Reply #3 on: September 23, 2008, 03:04:46 PM »
Dan's right and it's sometimes called 'constructor chaining'.

Glenn R

  • Guest
Re: Sincpac code question
« Reply #4 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.

Spike Wilbury

  • Guest
Re: Sincpac code question
« Reply #5 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") { ... }

Glenn R

  • Guest
Re: Sincpac code question
« Reply #6 on: September 23, 2008, 04:18:14 PM »
Spot on the money Luis...no defaulty argumentos for C#.

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Re: Sincpac code question
« Reply #7 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.
James Maeding

Glenn R

  • Guest
Re: Sincpac code question
« Reply #8 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.

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Re: Sincpac code question
« Reply #9 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.
James Maeding

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8788
  • AKA Daniel
Re: Sincpac code question
« Reply #10 on: September 23, 2008, 06:00:22 PM »

Glenn R

  • Guest
Re: Sincpac code question
« Reply #11 on: September 23, 2008, 06:21:39 PM »
me 3

Spike Wilbury

  • Guest
Re: Sincpac code question
« Reply #12 on: September 23, 2008, 06:25:57 PM »
not me, I know to much already....   :-P     :lmao:



and now seriously:



me cuatro.
« Last Edit: September 23, 2008, 06:45:03 PM by Luis »

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Re: Sincpac code question
« Reply #13 on: September 24, 2008, 04:29:57 PM »
come on Sincovec, "Me Sinco"...
James Maeding

sinc

  • Guest
Re: Sincpac code question
« Reply #14 on: September 24, 2008, 04:35:58 PM »
come on Sincovec, "Me Sinco"...

Septo you beat me to it...  Oucho these puns hurt.   :-)

sinc

  • Guest
Re: Sincpac code question
« Reply #15 on: September 24, 2008, 04:40:45 PM »
Oops, skipped six.  It must've been taking a seis-ta.