passing a record array as an argument to a procedure

I want to see if thsi is even possible.  I would like to set up a procedure that I can pass a variable lenth list of key/value pairs to..

so for example.....

  type TMyEventKeyValue = record
    evt_key   : String;
    evt_value : String;
  end;

  procedure LogMyEvent(Module : String; const Args: array of TMyEventKeyValue);
  var
    x : Integer;
  begin
    for x := 0 to High(Args) do
      begin
        ShowMessage(Args[x].evt_key+' => '+Args[x].evt_value);
      end;
  end;
LVL 6
rbohacAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mokuleCommented:
Where is the problem?

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

type TMyEventKeyValue = record
  evt_key   : String;
  evt_value : String;
end;

  procedure LogMyEvent(Module : String; const Args: array of TMyEventKeyValue);

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure LogMyEvent(Module : String; const Args: array of TMyEventKeyValue);
var
  x : Integer;
begin
  for x := 0 to High(Args) do
    begin
      ShowMessage(Args[x].evt_key+' => '+Args[x].evt_value);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Args: array[0..2] of TMyEventKeyValue;  // first index must be 0
begin
  Args[0].evt_key := 'a';
  Args[0].evt_value := '1';
  Args[1].evt_key := 'b';
  Args[1].evt_value := '2';
  Args[2].evt_key := 'c';
  Args[2].evt_value := '3';
  LogMyEvent('aaa', Args);
end;

end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TheRealLokiSenior DeveloperCommented:
Delphi already allows for "name/value" pairs with TStringlist.
Look at the following

procedure LogMyEvent(Module : String; Args: TStringList); // do not need const here since it is just a pointer
    var
        x : Integer;
    begin
        for x := 0 to Pred(Args.Count) do
        begin
            ShowMessage( Args.Names[x] + ' => ' + Args.Values[Args.Names[x]] ); // e.g. Args.Values['Age']
        end;
    end;

procedure TForm1.Button1Click(Sender: TObject);
    var
        sl: TStringList;
    begin
        sl := TStringList.Create;
        sl.Values['Name'] := 'Bob';
        sl.Values['Age'] := '100';
        LogMyEvent('Module', sl);
    end;
0
TheRealLokiSenior DeveloperCommented:
btw, I forgot to add
    sl.clear;
    sl.free;
at the end of my code example just to clean up nicely :-)
0
ZhaawZSoftware DeveloperCommented:
btw, I advise to use "length(args) - 1" instead of "high(args)" with dynamic arrays
>> for x := 0 to length(args) - 1 do

Or...
>> if length(args) > 0 then
>> for x := 0 to high(args)

"0 to high(args)" may result in exception if length of "args" array is 0
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.