TheSwamp
Code Red => .NET => Topic started 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.
-
It’s probably setting a default to another constructor in the class, i.e
PlotStyleCommandParameter(string parameterPrefix, bool flag)
-
something like
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;
}
}
-
Dan's right and it's sometimes called 'constructor chaining'.
-
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.
-
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:
void MyFunction(double num, int index, CString str="My Default") { ... }
-
Spot on the money Luis...no defaulty argumentos for C#.
-
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.
-
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.
-
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.
-
I have a lot to learn.
me too
-
me 3
-
not me, I know to much already.... :-P :lmao:
and now seriously:
me cuatro.
-
come on Sincovec, "Me Sinco"...
-
come on Sincovec, "Me Sinco"...
Septo you beat me to it... Oucho these puns hurt. :-)
-
Oops, skipped six. It must've been taking a seis-ta.