KE50
KE50

Reputation: 556

Delphi 7 Procedure with variant parameters

Have a procedure which looks like

Procedure TestProc(TVar1, TVar2 : variant);
Begin
  TVar1 := CreateOleObject('Excel.Application');
  TVar1.Workbooks.open('C:\Test\Test.xls');
  TVar1.Workbooks[1].Worksheets[1].Name := 'Sheet_1';
  TVar2 := TVar1.Workbooks[1].Worksheets['Sheet_1'];
End;

Note: TVar1 and TVar2 are global variables

Calling the procedure in an onclick event of a button and then using the created objects is not working

Is it that delphi does not allow creation of procedures having variant parameters???

Upvotes: 0

Views: 1276

Answers (1)

dthorpe
dthorpe

Reputation: 36072

In the code sample you present, TVar1 and TVar2 are not global variables, they are local parameters. They are not marked as var params, so they will receive a copy of whatever parameter values are passed into the function call, and any changes made to these local variables will not be passed back to the caller. It doesn't matter if there are also global variables named TVar1 and TVar2 declared elsewhere, in this procedure the local params will take precedence.

If you want to pass modifications back to the caller, declare the parameters as var parameters.

Procedure TestProc(var TVar1, TVar2 : variant);

Upvotes: 8

Related Questions