在Delphi中使用“with”创建的引用对象实例
发布时间:2020-12-15 04:07:20 所属栏目:大数据 来源:网络整理
导读:有没有办法引用使用“with”语句创建的对象实例? 例: with TAnObject.Create dobegin DoSomething(instance);end; DoSomething将使用实例引用,就像您将实例从变量声明引用传递给创建的对象一样. 例: AnObject := TAnObject.Create; 谢谢. 解决方法 那么,
有没有办法引用使用“with”语句创建的对象实例?
例: with TAnObject.Create do begin DoSomething(instance); end; DoSomething将使用实例引用,就像您将实例从变量声明引用传递给创建的对象一样. 例: AnObject := TAnObject.Create; 谢谢. 解决方法
那么,你可以使用这样的方法:
// 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”语句. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |