Generics Queue with Record , Enqueue Record 直接

Generics Queue with Record , Enqueue Record Directly

私人部门 :

type
  TWorkOwner = (woClient,woServer);

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

  WorkFlow : TQueue<TWorkData>;
  WorkData : TWorkData;

然后我希望像这样将 WorkData 类型的项目添加到此队列中:

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

  WorkFlow.Enqueue(WorkData);

这行得通,但我想(如果可能的话)像这样直接将 WorkData 入队(伪代码):

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

这显然行不通,我尝试了几种不同的方法,但我完全不知道如何设置它。

谢谢。

您可以定义记录的构造函数,如下所示:

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

然后Enqueue像这样:

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

P.S。从未尝试过,我在这种情况下使用 类。

最简单的方法是向采用这些参数的记录添加构造函数:

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));