delphi 匿名方法访问var参数

https://community.idera.com/developer-tools/general-development/f/rad-studio-general/72305/anonymous-methods-acessing-var-parameters
Anonymous methods acessing var parameters
Hi there

Is it possible to access or set the content of a var parameter inside a anonymous method? When I try this code below, the compiler doesn‘t recognize the variabel Value.

procedure TForm1.OnGetValue(var Value: Double);
begin
  Form2:=TForm2.Create(nil);
  Form2.ShowModal(procedure(AResult: TModalResult)
  begin
    if AResult=mrOK then
      Value:=Form2.FValue;
  end);
end;

Hi, try this

procedure TForm1.OnGetValue(var Value: Double);
var
  X : Double;
begin
  Form2 := TForm2.Create(Nil);
  Form2.ShowModal(
    procedure(AResult: TModalResult)
    begin
    if AResult=mrOK then
       X:=Form2.FValue;
    end);
  Value:=X;
end;

or this

procedure TForm1.OnGetValue(var Value: Double);
var
  X : PDouble;
begin

x:=@Value;
  Form2 := TForm2.Create(Nil);
  Form2.ShowModal(
    procedure(AResult: TModalResult)
    begin
    if AResult=mrOK then
       X^:=Form2.FValue;
    end);
end;

delphi 匿名方法访问var参数

上一篇:IPC(进程间的通讯方式)


下一篇:初学linux