Delphi Key Value Pairs

We get so used to the data structures in more modern languages that its frustrating using old Delphi 7 but I will show you how you can at least create a list map like structure using TStringList.

 var
  KeyValuePairsSample: TStringList;
  iIndex : integer;
  sname, svalue, svalueindex: string;
begin
  KeyValuePairsSample := TStringList.Create;
  KeyValuePairsSample.Clear;
  KeyValuePairsSample.NameValueSeparator := '=';
    KeyValuePairsSample.Add('ZERO=0'); // name=value pair //
  KeyValuePairsSample.Add('ONE=1');
  KeyValuePairsSample.Add('TWO=2');
  KeyValuePairsSample.Add('THREE=3');
  KeyValuePairsSample.Add('FOUR=4');
  KeyValuePairsSample.Add('FIVE=5');
  Memo1.Clear;
  Memo1.Lines.Add(KeyValuePairsSample.Names[1]);
  Memo1.Lines.Add(KeyValuePairsSample.Values['THREE']);
  Memo1.Lines.Add(KeyValuePairsSample.Names[0]);

  Memo1.Lines.Add(inttostr(KeyValuePairsSample.IndexOfName('THREE')));
 Memo1.Lines.Add(KeyValuePairsSample.ValueFromIndex[KeyValuePairsSample.IndexOfName('THREE')]);
  KeyValuePairsSample.free;
end;


Output:
ONE
3
ZERO
3
3

Leave a Reply

Your email address will not be published.


CAPTCHA Image
Reload Image