Recent

Author Topic: Read Section Entries from .ini-File  (Read 1777 times)

AWilhelm

  • Newbie
  • Posts: 2
Read Section Entries from .ini-File
« on: March 26, 2019, 09:55:17 am »
Hi, I'm pretty new to Lazarus,

i want to read all entries in a section in an .ini-File. I tried the following, but i always results in an error (Abstract method called)

Code: Pascal  [Select][+][-]
  1. //------------------------------------------------------------------------------------------------------------------------------------------------------------
  2. function GetIniSection( iniFilename, iniSect : string; tmpStrings : TStrings ) : boolean;
  3. //------------------------------------------------------------------------------------------------------------------------------------------------------------
  4. var inifile         : TMemIniFile;
  5. begin
  6.    inifile := TMemIniFile.create( iniFilename );
  7.    try
  8.       inifile.ReadSection( iniSect, tmpStrings );
  9.       result := true;
  10.    except
  11.       result := false;
  12.    end;
  13.    inifile.Free;
  14. end;    
  15.  

wadman

  • New Member
  • *
  • Posts: 37
    • wadman's home
Re: Read Section Entries from .ini-File
« Reply #1 on: March 26, 2019, 10:04:38 am »
Try replacing TStrings to TStringList.

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Read Section Entries from .ini-File
« Reply #2 on: March 26, 2019, 10:06:22 am »
The procedure which calls your GetIniSection must create the tmpStrings. But TStrings is an abstract class, it only serves as ancestor of advanced classes which do implement them. The most prominent class inheriting from TStrings is TStringList.

The calling procedure also must make sure that the created stringlist is destroyed again when it is no longer needed.

Therefore, your code should work when the calling procedure of GetIniSection does this:

Code: Pascal  [Select][+][-]
  1. var
  2.   tmpStrings: TStrings;
  3. begin
  4.   tmpStrings := TStringList.Create;
  5.   try
  6.     if GetIniSection('MyProgam.ini', 'MySection', tmpStrings) then
  7.       // ... do something with tmpStrings
  8.   finally
  9.     tmpStrings.Free;
  10.   end;

 

TinyPortal © 2005-2018