简体   繁体   English

带记录的泛型队列,直接入队记录

[英]Generics Queue with Record , Enqueue Record Directly

Private Section :私人部分

type
  TWorkOwner = (woClient,woServer);

  TWorkData = record
    Owner : TWorkOwner;
    AMessage : string;
    MsgID : integer;
  end;

  WorkFlow : TQueue<TWorkData>;
  WorkData : TWorkData;

Then I wish to add items of Type WorkData to this queue like this :然后我希望像这样将 WorkData 类型的项目添加到这个队列中:

  WorkData.Owner:=woClient;
  WorkData.AMessage:='LogOn';
  WorkData.MsgID:=MsgID;

  WorkFlow.Enqueue(WorkData);

This works but I would like (if possible) directly Enqueue WorkData like this (pseudo code) :这有效,但我想(如果可能的话)像这样直接 Enqueue WorkData (伪代码):

WorkFlow.Enqueue(woClient,'LogOn',MsgID);

This obviously does not work , I tried a few different approaches but I cannot figure out how to set this if at all possible.这显然不起作用,我尝试了几种不同的方法,但如果可能的话,我无法弄清楚如何设置它。

Thank you .谢谢你 。

You can define constructor of your record, something like this:您可以定义记录的构造函数,如下所示:

constructor Create(AOwner: TWorkOwner; AMessage: string; AMsgID: integer);

And then Enqueue it like this:然后Enqueue是这样的:

WorkFlow.Enqueue(TWorkData.Create(woClient,'LogOn',MsgID));

PS Never tried this, I use classes in such scenarios. PS 从来没有试过这个,我在这种情况下使用类。

The easiest approach is to add a constructor to the record that takes these parameters:最简单的方法是向采用这些参数的记录添加一个构造函数:

type
  TWorkData = record
    Owner: TWorkOwner;
    AMessage: string;
    MsgID: integer;
  public
    constructor Create(AOwner: TWorkOwner; const AAMessage: string; AMsgID:
        integer);
  end;

constructor TWorkData.Create(AOwner: TWorkOwner; const AAMessage: string;
    AMsgID: integer);
begin
  Owner := AOwner;
  AMessage := AAMessage;
  MsgID := AMsgID;
end;

WorkFlow.Enqueue(TWorkData.Create(woClient,'LogOn',MsgID));

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

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