Microsoft KB Archive/305814

= How to pass optional method arguments from C# =

Article ID: 305814

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q305814





SUMMARY
This step-by-step article shows you how to take advantage of optional method arguments. C# does not support optional method arguments. However, there may be times when you are using components that were created in a language that supports optional arguments, such as legacy COM components or components created with Microsoft Visual Basic .NET.

For the purpose of illustration, the Navigate2 method of SHDocVw.IWebBroswer2 is used. Only the first of the five arguments to this method is required; the remaining four are optional.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET

Declare an object of type &quot;Missing&quot;
object m = Type.Missing;

Pass the &quot;Missing&quot; object as an argument
The Navigate2 method requires that arguments be passed by reference. Check the documentation for the method that you are using to determine if the arguments need to passed by value or by reference. ie.Navigate2(ref url,ref m,ref m,ref m,ref m);

Set project references (optional)
The code sample in the &quot;Complete code sample&quot; section is used to illustrate this technique. This code sample does not run as is. If you want to try this code, you have to perform two additional steps:  Add a project reference to Shdocvw.dll. This is listed as Microsoft Internet Controls in the COM section of the Add Reference dialog box.  Precede the class declaration in your code with the following: using SHDocVw; 

Complete code sample
object m = Type.Missing; object url = &quot;http://www.microsoft.com&quot;; InternetExplorer ie = new InternetExplorer; ie.Navigate2(ref url,ref m,ref m,ref m,ref m); ie.Visible = true;

Keywords: kbhowtomaster kbprod2web KB305814

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.