Research on the use of optional parameters in COM
Author: wangzhidong (Steven Bob)
Creative Date: 2002-3-27
When designing the COM component method in Delphi6, in Parameter Flags Dialog, select the optional option, that is, the input parameter is optional, and now give the optional parameter instance.
Monitoring function
Function VariSemptyParam (Const v: Variant): Boolean;
Begin
Result: = (tvardata (v) .vtype = varrror) and
(Tvardata (v) .verror = $ 80020004); {DISP_E_PARAMNOTFOUND}
END;
Example
Uses Comserv, Variants, Dialogs, SysuTils;
Procedure ToptionalParamsdemo.getParams (Const Param1: WideString; Param2, Param3: Olevariant; param4: integer;
VAR
S2, S3: WIDESTRING; Begin
IF varisemptyparam (param2) THEN
S2: = 'EMPTY'
Else
S2: = 'NOT EMPTY';
IF varisemptyparam (param3) THEN
S3: = 'EMPTY'
Else
S3: = 'NOT EMPTY';
ShowMessage (Format ('param1 [% s] param2 [% s] param3 [% s] param4 [% d], [param1, s2, s3, param4]));
END;
Procedure TForm1.Button1Click (Sender: Tobject); var v: variant; begin v: = idispatch (cooptionalparamsdemo.create ()); v.GetParams ('123');
procedure TForm1.Button2Click (Sender: TObject); var AIntf: IOptionalParamsDemo; V: Variant; begin AIntf: = CoOptionalParamsDemo.Create (); // AIntf.GetParams ( '', EmptyParam, 9, 4); V: = IDispatch ( AINTF); // v.GetParams ('123', '123', '123', '123'); v.GetParams (); END;
procedure TForm1.Button3Click (Sender: TObject); var AIntf: IOptionalParamsDemo; V: Variant; begin AIntf: = CoOptionalParamsDemo.Create (); // AIntf.GetParams ( '', EmptyParam, 9, 4); V: = IDispatch ( AINTF); V.GETPARAMS ('123', '123'); // v.getParams (); END;
procedure TForm1.Button4Click (Sender: TObject); var V: Variant; begin V: = IDispatch (CoOptionalParamsDemo.Create ()); V.GetParams ( '123', '123', 123); end; procedure TForm1.Button5Click ( Sender: Tobject); var v: variant; begin v: = idispatch (coilparamsdemo.create ()); v.GetParams ('123', 123, 123, 6); end; example download: http://www.megspace .com / Computers / CoreWare /