- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人向我介绍了 TObjectList,我想使用它,但我似乎无法获得 sample code from Embarcadero's web site为我工作。这是我的代码:
unit Test03Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
{ Declare a new object type. }
TNewObject = class
private
FName: String;
public
constructor Create(const AName: String);
destructor Destroy(); override;
end;
{ TNewObject }
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor TNewObject.Create(const AName: String);
begin
FName := AName;
end;
destructor TNewObject.Destroy;
begin
{ Show a message whenever an object is destroyed. }
MessageDlg('Object "' + FName + '" was destroyed!', mtInformation, [mbOK], 0);
inherited;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
List: TObjectList<TNewObject>;
Obj: TNewObject;
begin
{ Create a new List. }
{ The OwnsObjects property is set by default to true -- the list will free the owned objects automatically. }
List := TObjectList<TNewObject>.Create();
{ Add some items to the List. }
List.Add(TNewObject.Create('One'));
List.Add(TNewObject.Create('Two'));
{ Add a new item, but keep the reference. }
Obj := TNewObject.Create('Three');
List.Add(Obj);
{
Remove an instance of the TNewObject class. Destructor
is called for the owned objects, because you have set the OwnsObjects
to true.
}
List.Delete(0);
List.Extract(Obj);
{ Destroy the List completely--more message boxes will be shown. }
List.Free;
end;
end.
尝试编译此文件时,出现错误 [DCC Error] Test03Unit1.pas(51): E2003 未声明的标识符:'TObjectList<>'。第 51 行内容如下:
List: TObjectList<TNewObject>;
我以前从未见过 Pascal 语言中使用过 < >,所以这对我来说是全新的领域。在谷歌上搜索“Delphi and < >”似乎并没有告诉我我需要了解什么。来自 other examples I can find on the internet这似乎是正确的使用方法。
使用德尔福XE2。
我做错了什么?
最佳答案
您必须添加 System.Generics.Collections
到您的使用条款。这是声明 TObjectList<T>
的单位.
我已将文档链接添加到答案中。当您找不到某个类时,请在文档中查找。这将告诉您需要使用哪个单位。
以及 TObjectList<T>
有TList<T>
。使用 TObjectList<T>
是有意义的当您希望列表拥有其成员时。否则,使用TObjectList<T>
没有任何好处。你也可以使用TList<T>
。除了内置的Delphi通用容器之外,还有许多优秀的第三方容器,它们往往更优秀。例如,Delphi Spring Framework有一个很好的容器集合。
关于delphi - TObjectList E2003 未声明的标识符 TObjectList<>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20076655/
有人向我介绍了 TObjectList,我想使用它,但我似乎无法获得 sample code from Embarcadero's web site为我工作。这是我的代码: unit Test03Un
请参阅下面的代码。为了简单起见,我删除了很多代码,但是剩下的代码仍然很长,抱歉:( IObserver = interface ['{1DD212F8-BD5E-47BF-9A3B-39E
我的代码有问题,它使用泛型类型。为什么编译器不知道传递的列表( Result )是 TObjectList ( TItem 是 T 中 TItems 的类型)? 界面: type TItem =
总结: TList.IndexOf(TList 在 Classes.pas 单元中定义)线性迭代所包含的项目,并比较引用。 TList.IndexOf(在 Generics.Collections.p
我认为我需要朝正确的方向插入: 我有两个相同数据类型的 Tobjectlist,我想将它们连接到一个新列表中,其中 list1 将被复制(未修改),然后是 list2(相反) type TMyLi
我正在尝试在 Delphi XE8 中创建 TObjectList 类,但是当我尝试获取值时出现错误。 compiler error message: "[dcc32 Error] : can't a
我想使用内置排序方法对通用 tobjectlist 进行排序。 这就是我所做的: //create the list object myList := TObjectList.Create(false
我在 Delphi 7 中有这段代码: var Form1: TForm1; T: TObjectList; implementation {$R *.dfm} procedure TForm
我需要了解如何使用通用 Delphi 2009 TObjectList .我的非TObjectList尝试看起来像 TSomeClass = class(TObject) private FLis
我有一个 OwnsObjects = true 的 TObjectList。它包含相当多的对象。现在我想从该列表中删除索引 Idx 处的对象,而不释放它。 Extract 方法是唯一的选择吗? Ext
这个例子当然是简化的,但基本上我有一个主窗体,它用 触发另一个窗体(frmSettings) function Execute(var aSettings: TSettings):Boolean TS
我使用 TObjectList 来处理特定的标签列表。但是当我在 Try Final 中执行推荐的 .Free 时,它还会从列表中删除控件。 带有 3 个标签的简单示例: Procedure Ge
我有以下 Delphi 代码: destructor TXX_XXXX.Destroy; var i: Integer; begin if Assigned(Allocations) then
我在大型应用程序中遇到了一个非常奇怪的问题。我大量使用 TObjectList 在其上存储自定义对象。在大型列表中,当我使用 CLEAR 方法时,我遇到了奇怪的崩溃,“地址访问冲突..读取地址..”“
我对 Spring4D 框架的 TObjectList 类的行为有疑问。在我的代码中,我创建了一个几何图形列表,例如方形、圆形、三角形,每个图形都定义为单独的类。为了在列表被销毁时自动释放几何图形,我
我有一个包含一个或多个对象的 TObject 列表 (FileEventObjects := TObjectList.Create(True);)。这些对象需要保留在列表中,直到它们被处理。 (对象列
我正在使用 TObjectList (Delphi 2007) 来存储大量数据 - 我预计有大约 30 万个元素甚至更多。但是,当创建列表时,它的默认大小设置为仅存储四个元素,如果尝试添加第五个元素,
我有一个这样的列表: FMyScheduleList: TObjectList; 它有一个属性: property ADate: TDate read FDate write FDate; 如何按此属
我对 Delphi 中的泛型仍然有点模糊,但一直在使用 TObjectList<>相当广泛。现在我遇到的情况是,我有一个具有此类私有(private)字段的基类,但需要为任意类创建,并且该类也是从另一
在 Delphi 2009 中,到目前为止,我在泛型方面没有遇到重大问题(使用 Generics.Collections 列表,没有特殊的泛型功能)。 现在我发现这段代码会在访问 MyList.Cou
我是一名优秀的程序员,十分优秀!