Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optional Parameters in Automation(exposed method)

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
trying to figure outhow to make an exposed method with optional parameters <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
I figured it out<br><br>short CWiDataClientCtrl::Get(LPCTSTR source, const VARIANT FAR& dest) <br>{<br>if(dest.vt == VT_ERROR)<br> lstrcpy(m_dclient.destR,&quot;no&quot;);<br>else<br>{<br> _bstr_t Bstr(dest);<br> lstrcpy(m_dclient.destR,(char*)Bstr);<br>}<br>....<br>The optional parameter must be a variant , heres the information from MSDN ID : Q154039<br><i><b><br>HOWTO: Pass Optional Arguments to MFC ActiveX Controls<br>ID: Q154039 <br><br>&nbsp;<br><br>--------------------------------------------------------------------------------<br>The information in this article applies to:<br><br>The Microsoft Foundation Classes (MFC), included with:<br>Microsoft Visual C++, 32-bit Editions, versions 4.0, 4.1, 4.2, 5.0, 6.0<br><br>--------------------------------------------------------------------------------<br><br><br>SUMMARY<br>Many automation methods on ActiveX Controls do not require that all possible arguments are passed with each call. Arguments that are not required are called optional arguments. In MFC, it is possible to add a method to a custom control that accepts optional arguments, but there are a few rules that must be followed. This article outlines these rules, and provides step-by-step instruction on how to add such a method to a control. <br><br><br><br>MORE INFORMATION<br>MFC's implementation of IDispatch accepts optional arguments according to the following rules: <br><br><br><br>Optional arguments must appear at the end of the parameter list. If an optional argument is omitted when calling the method, all arguments to the right of this one must also be omitted. This is similar to the C++ rules for calling functions with Default values for parameters. <br><br><br>Optional arguments must be declared as VARIANTs. If a parameter is omitted, the calling program must pass a VARIANT initialized as: <br><br>&nbsp;&nbsp;&nbsp;VARIANT.vt = VT_ERROR<br>&nbsp;&nbsp;&nbsp;VARIANT.scode = DISP_E_PARAMNOTFOUND. <br>This will be done automatically by Visual Basic. However, in Visual C++ you must create and initialize the VARIANT yourself. <br><br>NOTE: This implies that the method must contain code that checks the VARIANT to see if it contains actual data or is just a place holder. <br><br><br>You must edit the ODL file generated for the control and mark the parameters as optional. This is done by adding the [optional] tag as shown here: <br><br>&nbsp;&nbsp;&nbsp;[id(1)] void MyOpt([optional] VARIANT param1,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[optional] VARIANT param2); <br>If you don't add the [optional] tag, Visual Basic will put up a message box indicating &quot;Parameter Not Optional&quot; when you call the method with one or more parameters omitted. <br><br><br>To add a method that supports optional arguments, follow these steps: <br><br><br>Using ClassWizard, add a new method to the control. Be sure to pick VARIANT as the type for each optional argument, and be sure the optional arguments appear last in the parameter list. <br><br><br>Edit the ODL file, and mark these parameters with the [optional] tag. <br><br><br>Add the appropriate functionality to the method and rebuild the control. <br><br>For example, the MyOpt method defined in ODL syntax above could be called from Visual Basic in the following three forms: <br><br>&nbsp;&nbsp;&nbsp;myocx.MyOpt 2, 3&nbsp;&nbsp;'passing both args<br>&nbsp;&nbsp;&nbsp;myocx.MyOpt 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'omit one arg<br>&nbsp;&nbsp;&nbsp;myocx.MyOpt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'omit both args <br></b></i><br> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top