[英]Delphi - Using TStringList in Class Definition (very new)
我在Delphi中做一个简单的类定义,我想在类及其构造函数中使用TStringList
(因此,每次创建对象时,都将其传递给StringList
,它会对StringList
数据做一些神奇的事情,复制字符串列表到它自己的内部字符串列表)。 我得到的问题是,当我尝试在类定义之前声明它“使用”的内容时(因此它知道如何处理TStringList
),编译时将失败。 但是,如果不这样做,它就不知道TStringList
是什么。 因此,这似乎是一个范围界定的问题。
下面是一个(非常简化的)类定义,类似于我正在尝试做的事情。 有人可以建议我如何进行这项工作并确定范围吗?
我也尝试在项目级别添加use语句,但是仍然失败。 我不知道该怎么做才能做到这一点。
unit Unit_ListManager;
interface
type
TListManager = Class
private
lmList : TStringList;
procedure SetList;
published
constructor Create(AList : TStringList);
end;
implementation
uses
SysUtils,
StrUtils,
Vcl.Dialogs;
constructor TBOMManager.Create(AList : TStringList);
begin
lmList := TStringList.Create;
lmList := AListList;
end;
procedure SetPartsList(AList : TStringList);
begin
lmList := AListList;
ShowMessage('Woo hoo, got here...');
end;
end.
亲切的问候
您没有显示添加单位引用的确切位置,但是我敢打赌这是错误的地方。 注意interface
和type
之间的其他代码。
我也更正了您对constructor
的定义,您将其放置在published
而非public
。 只有property
项属于已published
部分。
unit Unit_ListManager;
interface
uses
Classes,
SysUtils,
StrUtils,
Vcl.Dialogs;
type
TListManager = Class
private
lmList : TStringList;
procedure SetList;
public
constructor Create(AList : TStringList);
end;
implementation
constructor TListManager.Create(AList : TStringList);
begin
inherited Create; // This way, if the parent class changes, we're covered!
// lmList := TStringList.Create; This would produce a memory leak!
lmList := AListList;
end;
procedure TListManager.SetList;
begin
// You never provided an implementation for this method
end;
end.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.