简体   繁体   English

在运行时创建对象命名

[英]Creating objects at runtime Nameing

Say i have a class called TMachine I currently create 1 class like so 说我有一个叫TMachine的类,我现在像这样创建1个类

 Machine := MachineShape.TMachine.create(self);

but I need more then 1, never really sure how many i need as it depends on # of machines in the database at the time, Should never be over 20. So I know i need an array of some type in the var section. 但是我需要的数量超过1,所以从不知道我当时需要多少数量,这取决于当时数据库中的计算机数量,应该永远不超过20。所以我知道在var部分中需要某种类型的数组。 Currently i have 目前我有

procedure TFLayout1.GetClick(Sender: TObject);
var
  machine : TMachine;
begin
 .....

  //gets number of machines in total
  while not fdeptlayout.ADOQuery1.Eof do
    begin
      fdb.count := fdb.count+1;
      fdeptlayout.ADOQuery1.Next;
    end;

  //restarts back at first query
  fdeptlayout.ADOQuery1.First;


  //creates the shape
  while not fdeptlayout.ADOQuery1.Eof do
    begin
        machine := MachineShape.TMachine.Create(self);
        machine.PlaceShape(44,44,'CM402','first','123/33/123');
        fdeptlayout.ADOQuery1.Next;
    end;
end;

currently that will place 1 machine on the form "Machine" . 目前,这将以"Machine"的形式放置1台"Machine" I Need it to place "Machine1" "Machine2" ... for how ever many are in the query. 我需要将“ Machine1”,“ Machine2” ...放置在查询中。 Thus "machine" needs replaced by an array some how? 因此"machine"需要用数组代替一些怎么办? everything is created at runtime. 一切都在运行时创建。

The simplest is to use a list. 最简单的是使用列表。 Use a generic list from Generics.Collections . 使用Generics.Collections的通用列表。

FList: TList<TMachine>;

For you needs, you would declare the list to be a field in one of your classes. 根据您的需要,您可以将列表声明为您的一个课程中的一个字段。 The class that you want to control and manage all the machines. 您要控制和管理所有计算机的类。

Create it like this: 像这样创建它:

FList := TList<TMachine>.Create;

Add new members to the list like this: 像这样将新成员添加到列表中:

FList.Add(Machine);

Iterate over the list like this: 像这样遍历列表:

for Machine in FList do
  Machine.DoSomething;

If you would like the list to control the lifetime of the machines, then use TObjectList<T> instead. 如果您希望该列表控制计算机的生存期,请改用TObjectList<T>

FList: TList<TMachine>;
....
FList := TObjectList<TMachine>.Create;

Then when you remove items from the list, clear the list, destroy the list etc., the machines will also be destroyed. 然后,当您从列表中删除项目,清除列表,销毁列表等时,机器也将被销毁。

The Delphi documentation contains an example illustrating the use of TObjectList<T> . Delphi文档包含一个示例,示例说明了TObjectList<T>的用法


So, to make it clear, your while loop would look like this: 因此,为了清楚起见,您的while循环如下所示:

while not fdeptlayout.ADOQuery1.Eof do
begin
  machine := MachineShape.TMachine.Create(self);
  FList.Add(machine);
  machine.PlaceShape(44,44,'CM402','first','123/33/123');
  fdeptlayout.ADOQuery1.Next;
end;

Then at some other point in your code when you need to iterate over all machines, use the code in the for loop above. 然后,当您需要遍历所有机器时,在代码的其他位置,请使用上面的for循环中的代码。


Of course, this may all be a complete red herring. 当然,这可能全部是完全的红鲱鱼。 Perhaps you don't ever need to refer to your machine objects outside the routine in the question. 也许您永远不需要在问题例程之外引用您的机器对象。 In which case you don't need a list at all, and the code in the question is already perfectly adequate. 在这种情况下,您根本不需要列表,问题中的代码已经足够了。

I note that you state: 我注意到您声明:

Currently that will place one machine on the form. 目前,这会将一台机器放在表格上。

That is not so. 事实并非如此。 The while loop will create one machine object for each iteration of your while loop. while循环将创建一个计算机对象为您的每一次迭代while循环。 The fact that you re-use the local variable machine doesn't change that. 您重新使用局部变量machine事实不会改变这一点。 Each time round the loop you call TMachine.Create and so create a new instance each iteration. 每次循环时,您都调用TMachine.Create ,因此每次迭代都创建一个新实例。

You can place the machines in a TObjectList 您可以将机器放置在TObjectList中

In your class definition add 在类定义中添加

FMachines: TObjectList<TMachine>;

Then in your FormCreate: 然后在您的FormCreate中:

FMachines:= TObjectList<TMachine>.Create;

and in FormDestroy: 并在FormDestroy中:

FMachines.Free;

In your GetClick method dd the created machine to the list like so: 在您的GetClick方法dd中,将创建的机器添加到列表中,如下所示:

machine := MachineShape.TMachine.Create(self);
FMachines.Add(machine);

You can then refer to the created machines by accessing the FMachines list. 然后,您可以通过访问FMachines列表来引用创建的计算机。

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

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