delphi 在类定义中使用TStringList

tvmytwxo  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(80)

我在 Delphi 中做一个简单的类定义,我想在类和它的构造函数中使用TStringList(所以每次你创建一个对象,你传递给它一个StringList,它对StringList数据做一些神奇的事情,将字符串列表复制到它自己的内部字符串列表)。
我遇到的问题是,当我试图在类定义之前声明它“使用”什么时(这样它就知道如何处理TStringList),它在编译时失败了。但如果没有这样做,它就不知道TStringList是什么。所以这似乎是一个作用域问题。
下面是一个(非常简化的)类定义,类似于我正在尝试做的。有人能建议我如何使它工作并正确地确定作用域吗?
我也尝试在项目级别添加uses语句,但仍然失败。我想知道我需要做什么才能做到这一点。

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.

字符串

oprakyz7

oprakyz71#

您没有显示添加单位引用的确切位置,但我敢打赌这是错误的位置。请注意interfacetype之间的附加代码。
我还纠正了你对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.

字符串

相关问题