title: How can I search faster for name/value pairs in a Delphi TStringList?
body: <p>I implemented language translation in an application by putting all strings at runtime in a TStringList with:</p>
  
  <pre><code>procedure PopulateStringList;
  begin  
    EnglishStringList.Append('CAN_T_FIND_FILE=It is not possible to find the file');
    EnglishStringList.Append('DUMMY=Just a dummy record');
    // total of 2000 record appended in the same way
    EnglishStringList.Sorted := True; // Updated comment: this is USELESS!
  end;
  </code></pre>
  
  <p>Then I get the translation using:</p>
  
  <pre><code>function GetTranslation(ResStr:String):String;
  var
    iIndex : Integer;
  begin
    iIndex := -1;
    iIndex :=  EnglishStringList.IndexOfName(ResStr);
    if iIndex &gt;= 0 then
    Result :=  EnglishStringList.ValueFromIndex[iIndex] else
    Result := ResStr + ' (Translation N/A)';
  end;
  </code></pre>
  
  <p>Anyway with this approach it takes about 30 microseconds to locate a record, is there a better way to achieve the same result?</p>
  
  <p><strong>UPDATE: For future reference I write here the new implementation that uses TDictionary as suggested (works with Delphi 2009 and newer)</strong>:</p>
  
  <pre><code>procedure PopulateStringList;
  begin  
    EnglishDictionary := TDictionary&lt;String, String&gt;.Create;
    EnglishDictionary.Add('CAN_T_FIND_FILE','It is not possible to find the file');
    EnglishDictionary.Add('DUMMY','Just a dummy record');
    // total of 2000 record appended in the same way
  end;
  
  
  function GetTranslation(ResStr:String):String;
  var
    ValueFound: Boolean;
  begin
    ValueFound:=  EnglishDictionary.TryGetValue(ResStr, Result);
    if not ValueFound then Result := Result + '(Trans N/A)';
  end;
  </code></pre>
  
  <p>The new GetTranslation function performs 1000 times faster (on my 2000 sample records) then the first version.</p>