[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

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;
0
rbohac
Asked:
rbohac
  • 2
2 Solutions
 
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
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now