default parameter values 
Author Message
 default parameter values

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?



Sun, 21 Nov 2004 17:52:13 GMT  
 default parameter values
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]


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;
    ...

Quote:
}

// 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?



Sun, 21 Nov 2004 20:53:46 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. WORKAROUND: nested template type default parameter values

2. ActiveX default parameter values

3. HOWTO: Default Parameter values and refs and ADO.NET

4. function parameters: default value, optional parameter

5. template template parameter with default value (VS.NET 2003)

6. Function parameter default value?

7. Parameter default values

8. Emulate Named Parameters and Default Values

9. Need Default values for Interface Parameters ....

10. Default values for parameters?

11. Template argument dependant default parameter

12. How to declare function with default parameters ?

 

 
Powered by phpBB® Forum Software