Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The default value needs to be specified on the first method called (i.e. your wrapper) as this is where the value is applied to the parameter. <code>Type.Missing</code> is a special value that has meaning in COM interop. Here are some options you could try that may suit your needs.</p> <ol> <li><p>Use the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.optionalattribute.aspx" rel="nofollow">OptionalAttibute</a> on the base method and specify the default value on the overridden method.</p> <pre><code>class Class2 : Class1 { public override string MethodWithOptParams(string message = "default message") { return base.MethodWithOptParams(message); } } class Class1 { public virtual string MethodWithOptParams([Optional]string message) { return message; } } </code></pre></li> <li><p>Declare your default values as constants in and apply the same constant as the default value.</p> <pre><code>class Class2 : Class1 { public override string MethodWithOptParams(string message = DefaultMessage) { return base.MethodWithOptParams(message); } } class Class1 { protected const string DefaultMessage = "default message"; public virtual string MethodWithOptParams(string message = DefaultMessage) { return message; } } </code></pre></li> <li><p>Use null as the default value in your wrapper and code the two alternative calls to the base method.</p> <pre><code>class Class2 : Class1 { public override string MethodWithOptParams(string message = null) { if (message == null) { return base.MethodWithOptParams(); } else { return base.MethodWithOptParams(message); } } } class Class1 { public virtual string MethodWithOptParams(string message = "default message") { return message; } } </code></pre></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload