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

Delphi : Convert a String to TDateTime


In Delphi concerting string to TDateTime is always an issue for me: Here is the code to help someone.


var
   myformat : TFormatSettings;
   theDate : TDateTime;
begin

   GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, myformat );
   myformat.DateSeparator := '/';
   myformat.TimeSeparator := ':';
   myformat.ShortDateFormat := 'dd/mm/YYYY hh:nn';
 theDate  := StrToDateTime( '01/01/2017',myformat);
end;

Hope this helps someone.

The inverse is easier:


MyStr := FormatDateTime('dd/mm/yyyy HH:nn:ss',theDate));

If you have the Project Jedi Components JVCL Delphi Jedi you can use the

JvJCLUtils.StrToDateFmt ( 'yyyy/mm/dd', '2017/01/01' );

from the JvJCLUtils.pas file.