Free Resources from FMS

Additional Resources

 

Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.

Darren D.

 

Free Catalog

 

Call a COM or VB.NET Method with Optional Parameter(s) from C#

 

Provided by:  FMS Development Team

 

Q:

I have to make call to VB.NET or legacy COM components (Interop wrapper will be created for it.). Fairly often, there are methods with optional parameters. Unlike VB.NET or VB, C# does not support optional parameter. Then, how should I call these methods?

A:

C# forces you to specify all the parameters when calling a method. This means that you have to pass in something for the optional parameter.


You could look into document to figure out the default value of the parameter and pass it in.

Sometimes, the value you passed in does not matter. You can pass in a dummy value. Make sure pass in the right data type.

If the parameter is using the type "object" (or "variant" from VB/COM standpoint), there is a shortcut: you can just pass in this value:

System.Reflection.Missing.Value

If you need do this often, you could define it as a read-only member variable and use this variable instead, like this:

private readonly object def = System.Reflection.Missing.Value;

Return to the tips page