开发者

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

I am trying to write a procedure in delphi. Th开发者_如何学编程is 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.


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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜