“Types of actual and formal var parameters must be identical” error in Procedure

StackOverflow https://stackoverflow.com/questions/4411603

  •  08-10-2019
  •  | 
  •  

Question

I am trying to write a procedure in delphi. This procedure gets the name of TControl descendent element and then try to change some properties. But when i try to do it, Delphi gives an error like:

E2033 Types of actual and formal var parameters must be identical

Procedure:

procedure Change_prop(var Control: TControl;height:integer;width:integer);
begin
//......
end;

Usage Example : Change_prop(Label1, 50,200); What can be the solution of that error..Thanks.

Was it helpful?

Solution

You just need to remove the var in the Control parameter and make it a value parameter. Because Delphi objects are actually implemented as reference types, you can call methods on them, change member fields etc. even if you pass them to a procedure as a value or const parameter.

OTHER TIPS

Just remove var - you don't need it to change Control's properties:

procedure Change_prop(Control: TControl;height:integer;width:integer);
begin
......
end;

As David said, the problem's in the var. That doesn't mean that you can modify the members of the TControl, it means you can replace the TControl with another, completely different TControl, because objects are reference types.

If you're passing an object to a var parameter, the variable you pass has to be declared as exactly the same type as the parameter in order to preserve type safety. Otherwise, you could do this:

procedure TForm1.VarControl(var control: TControl);
begin
  control := TButton.Create;
end;

procedure TForm1.Mistake;
begin
  VarControl(self.Memo1); //defined as TMemo
  Memo1.Lines.Text := 'Undefined behavior here...';
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top