Home > database >  Generics Queue with Record , Enqueue Record Directly
Generics Queue with Record , Enqueue Record Directly

Time:10-13

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.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) :

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 .

CodePudding user response:

You can define constructor of your record, something like this:

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

And then Enqueue it like this:

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

P.S. Never tried this, I use classes in such scenarios.

CodePudding user response:

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