6
votes

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.

3

3 Answers

11
votes

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.

5
votes

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

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

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;