Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

default parameter values

0 views
Skip to first unread message

earwicker

unread,
Jun 5, 2002, 5:52:13 AM6/5/02
to
All:
 
I know that C# does not directly support C++-style default parameters of the form:
public void MyFunc( int inVal1, int inVal2 = 52 ) {...}
However, the 'workaround' that MS suggests in the documentation is to compensate for this problem using overrides, i.e.:
 
// version 1
public void MyFunc( int inVal1 )
{
    // here's my default value
    int inVal2 = 52;
    ...
}
 
// version 2, with user-supplied value
public void MyFunc( int inVal1, int inVal2 ) { ... }
 
Of course, this DOES work, but it's a ridiculous design solution in many cases.
 
In my current project, I have a Write() function that ALREADY has almost 20 overrides to account for different input Types (a la Console.Write() in .NET) . . . In addition, I need a boolean argument that specifies whether or not the Terminal's cursor should/should not be relocated after the Write() operation completes. This parameter should default to "true." Using the MS suggestion would double the current number of overrides, which--I hope you'll agree--is kinda goofy.
 
Any better ideas for a solution to the Daunting Dilemma of the Default Parameter Value?
 
Thanks,
 
---earwicker
 
ps
 
Any comments on WHY this exceptionally useful C++ syntax was left out/removed/discarded?

Nicholas Paldino [.NET/C# MVP]

unread,
Jun 5, 2002, 8:53:46 AM6/5/02
to
earwicker,

You will not be able to get around this in C#.

In VB, you could use the Optional keyword, but that will not create two
versions of your function (it places an attribute [opt] on the parameter),
which I believe is what you want. I imagne that Managed Extensions for C++
will give you the same result.

You could always place a property on your class that takes a boolean
value. This property, if set to true, would move the cursor appropriately
after the call to Write.

--
- Nicholas Paldino [.NET MVP]
- nicholas...@exisconsulting.com

"earwicker" <snowball...@hell.com> wrote in message
news:OWImrZHDCHA.2212@tkmsftngp02...

0 new messages