Reference object instance created using “with” in Delphi

后端 未结 9 1335
生来不讨喜
生来不讨喜 2020-12-03 18:17

is there a way to reference an object instance that is created using the \"with\" statement?

Example:

with TAnObject.Create do
begin
  DoSomething(in         


        
9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 19:23

    An addition to Brian's example on a Notify handler is to use an absolute variable (win32 only):

    procedure Notify( Sender : TObject ); 
    var 
      Something : TSomeThing absolute Sender;
    begin 
      if Sender is TSomething then 
      begin
        VerySimpleProperty := Something.Something;
        OtherProperty := Something.SomethingElse;
      end;
    end;
    

    It basically avoids having to assign a local variable or have a lot of type casts.

提交回复
热议问题