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

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

问题描述

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



示例:

  with TAnObject.Create do 
begin
DoSomething(instance);
结束

其中DoSomething将使用实例引用,就像您将一个实例从变量声明的引用传递给对象创建。



示例:

  AnObject:= TAnObject.Create ; 

谢谢。

解决方案

嗯,你可以使用这样的方法:

  //实现:

键入
TSimpleMethod =对象的过程;

函数GetThis(const pr:TSimpleMethod):TObject;
begin
结果:= TMethod(pr).Data;
结束

//用法:

与TStringList.Create do
try
CommaText:='1,2,3,4,5,6, 7,8,9,0';
ShowText(TStringList(GetThis(Free)));
终于
免费;
结束

或类助手:

 键入
TObjectHelper =类助手对于TObject
private
函数GetThis:TObject;一致;
public
属性This:TObject读取GetThis;
结束

...

函数TObjectHelper.GetThis:TObject;
begin
结果:=自我;
结束

但实际上,以前的回复是正确的:你最好忘记with语句。 p>

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

Example:

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

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

Example:

AnObject := TAnObject.Create;

Thanks.

解决方案

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;

or class helpers:

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;

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

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

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