Question

I wanna show 2 different results in delphi with my stringlist.

If the index is smaller than 1 it should show an other result.

I always get a List index exceeds the maximum error

Heres my Code.

function TForm1.readTable: String;
var
  ini: TIniFile;
  einträge: TStringList;
begin
  try
  einträge := TStringList.Create;
  inipfad := ExtractFilePath(ParamStr(0)) + '/initest/' + 'config.INI';
  ini := TIniFile.Create(inipfad);
  ini.ReadSections(einträge);
  ShowMessage('geht');

  if einträge.IndexOf < 1 then
    Result := einträge[0]
  else
    Result := einträge[1];

  finally
    ini.Free;
    Einträge.Free;
  end;
end;

Changed now to

  if einträge.Count <= 0 then begin
    Result := einträge[0]
  end
  else
    Result := einträge[1];

And its working without the error. Is this a real solution?

Was it helpful?

Solution

Instead of eintrage.indexof<1 you need eintrage.count=1.

You should also check that eintrage.count is either 1 or 2 in case your list is empty after ini.ReadSections.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top