使用“with"创建的引用对象实例在德尔福 [英] Reference object instance created using "with" in Delphi

查看:29
本文介绍了使用“with"创建的引用对象实例在德尔福的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法引用使用with"语句创建的对象实例?

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

示例:

with TAnObject.Create do
begin
  DoSomething(instance);
end;

DoSomething 将在哪里使用实例引用,就像您将实例从变量声明的引用传递到创建的对象一样.

Where DoSomething would use the instance reference as if you were passing an instance from a variable declared reference to the object created.

示例:

AnObject := TAnObject.Create;

谢谢.

推荐答案

好吧,你可以用这样的方法:

Well, you can use such approach:

// implement:

type
  TSimpleMethod = procedure of object;

function GetThis(const pr: TSimpleMethod): TObject;
begin
  Result := TMethod(pr).Data;
end;

// usage:

  with TStringList.Create do
  try
    CommaText := '1,2,3,4,5,6,7,8,9,0';
    ShowText(TStringList(GetThis(Free)));
  finally
    Free;
  end;

或类助手:

type 
  TObjectHelper = class helper For TObject
  private
    function GetThis: TObject; Inline;
  public
    property This: TObject read GetThis;
  end;

...

function TObjectHelper.GetThis: TObject;
begin
  Result := Self;
end;

但是,实际上,以前的回复是正确的:您最好忘记with"语句.

But, actually, previous replies are correct: you should better forget about "with" statement.

这篇关于使用“with"创建的引用对象实例在德尔福的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆