繁体   English   中英

Delphi-在类定义中使用TStringList(全新)

[英]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.

亲切的问候

您没有显示添加单位引用的确切位置,但是我敢打赌这是错误的地方。 注意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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM