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.

[sourcecode language=”pascal” wraplines=”false” collapse=”false”]

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;
[/sourcecode]

Hope this helps someone.

The inverse is easier:
[sourcecode language=”pascal” wraplines=”false” collapse=”false”]

MyStr := FormatDateTime(‘dd/mm/yyyy HH:nn:ss’,theDate));
[/sourcecode]

If you have the Project Jedi Components JVCL Delphi Jedi you can use the
[sourcecode language=”pascal” wraplines=”false” collapse=”false”]
JvJCLUtils.StrToDateFmt ( ‘yyyy/mm/dd’, ‘2017/01/01’ );
[/sourcecode]
from the JvJCLUtils.pas file.