Board index » delphi » Help with TStringList (Second Request)

Help with TStringList (Second Request)

I would like to create a TStringList that contains values read in from a
Section of an INI file.  I have followed the examples in the help file but
I keep getting nothing but an Access Violation.  The TStringlist should be
available to all my forms/procedures.

I've managed to accomplish this much:
Var
IniFile: TIniFile;
begin
Inifile := Tinifile.Create('Simple.ini');
Inifile.ReadSection('Sites', ListBox1.Items);
Inifile.ReadSectionValues('Sites', Listbox2.Items);
IniFile.Free;
end;

I have tried the following: (Where SiteList is a TStringlist)

Inifile.ReadSection('Sites',SiteList);
Or
SiteList.Assign(ListBox1.Items);
Or
SiteList.AddStrings(ListBox1.Items);

I cannot get any of these to work.  Help would be appreciated.  Also, WHERE
should the declaration for SiteList be if I want access to it anywhere in
my program.
--
Kathleen MacMahon (s...@together.net)
http://www.rahul.net/galen/index.html

 

Re:Help with TStringList (Second Request)


Quote
Kathleen MacMahon wrote:

> I would like to create a TStringList that contains values read in from a
> Section of an INI file.  I have followed the examples in the help file but
> I keep getting nothing but an Access Violation.  The TStringlist should be
> available to all my forms/procedures.

> I've managed to accomplish this much:
> Var
> IniFile: TIniFile;
> begin
> Inifile := Tinifile.Create('Simple.ini');
> Inifile.ReadSection('Sites', ListBox1.Items);
> Inifile.ReadSectionValues('Sites', Listbox2.Items);
> IniFile.Free;
> end;

> I have tried the following: (Where SiteList is a TStringlist)

> Inifile.ReadSection('Sites',SiteList);
> Or
> SiteList.Assign(ListBox1.Items);
> Or
> SiteList.AddStrings(ListBox1.Items);

> I cannot get any of these to work.  Help would be appreciated.  Also, WHERE
> should the declaration for SiteList be if I want access to it anywhere in
> my program.
> --
> Kathleen MacMahon (s...@together.net)
> http://www.rahul.net/galen/index.html

This may be a silly question, but... has SiteList actually been created?
Viz:

TForm1.Form1Create(...);
var
  SiteList: TStringList;
begin
  ...
  SiteList := TStringList.Create;
  ...
end;

Recommended: to simplify your code, *don't* have SiteList (or, if
possible, any other variable) globally visible to all forms/procedures.
Pass it as an argument to anything that needs to use it.

Graham Meintjes

Re:Help with TStringList (Second Request)


In article <01bbe761$c1239840$b2362...@shae.together.net>,
   "Kathleen MacMahon" <s...@together.net> wrote:
]-
]-I would like to create a TStringList that contains values read in from a
]-Section of an INI file.  I have followed the examples in the help file but
]-I keep getting nothing but an Access Violation.  The TStringlist should be
]-available to all my forms/procedures.
]-
]-I've managed to accomplish this much:
]-Var
]-IniFile: TIniFile;
]-begin
]-Inifile := Tinifile.Create('Simple.ini');
]-Inifile.ReadSection('Sites', ListBox1.Items);
]-Inifile.ReadSectionValues('Sites', Listbox2.Items);
]-IniFile.Free;
]-end;
]-
]-I have tried the following: (Where SiteList is a TStringlist)
]-
]-Inifile.ReadSection('Sites',SiteList);
]-Or
]-SiteList.Assign(ListBox1.Items);
]-Or
]-SiteList.AddStrings(ListBox1.Items);
]-
]-I cannot get any of these to work.  Help would be appreciated.  Also, WHERE
]-should the declaration for SiteList be if I want access to it anywhere in
]-my program.

double check your spelling, and the path to your ini file.  when I tried
to reproduce your problem I "succeeded" fabulously -- every call to
ReadSection returned an empty list.  after a bit of thrashing around though,
I eventually noticed that I'd made a typo in writing the name of my .ini
file.  after fixing the typo I was able to use ReadSection to load a list
box, and a string list.

as far as where to declare SiteList...how many different units need visibility
to it?  if only one, then declare SiteList as a variable in the implementation
section of that unit.  if more than one, declare it in the interface section of
a unit that's used in common by all of the units that reference SiteList.  if
you don't have a unit that's already used by all of the units that need to see
SiteList, you can always make one.

Mark Vaughan

Re:Help with TStringList (Second Request)


You don't say, but it is probable that you are not creating the
TStringList object SiteList.

Try (in Delphi 2.xx)

unit Globals;

interface

uses
  classes;

var
  SiteList: TStringList;

implementation

initialization
  SiteList := TStringList.Create;

finalization
  SiteList.free;
  SiteList := nil;

end.

Then in the unit/form you are reading the IniFile, just "use" the
Globals unit.

In Delphi 1.xx, define and create the variable in Globals, and free it
in the Destroy method of your main form.
HTH
Greg Defina
gr...@hufest.tmx.com.au

On 11 Dec 96 12:42:15 GMT, "Kathleen MacMahon" <s...@together.net>
wrote:

Quote

>I would like to create a TStringList that contains values read in from a
>Section of an INI file.  I have followed the examples in the help file but
>I keep getting nothing but an Access Violation.  The TStringlist should be
>available to all my forms/procedures.

>I've managed to accomplish this much:
>Var
>IniFile: TIniFile;
>begin
>Inifile := Tinifile.Create('Simple.ini');
>Inifile.ReadSection('Sites', ListBox1.Items);
>Inifile.ReadSectionValues('Sites', Listbox2.Items);
>IniFile.Free;
>end;

>I have tried the following: (Where SiteList is a TStringlist)

>Inifile.ReadSection('Sites',SiteList);
>Or
>SiteList.Assign(ListBox1.Items);
>Or
>SiteList.AddStrings(ListBox1.Items);

>I cannot get any of these to work.  Help would be appreciated.  Also, WHERE
>should the declaration for SiteList be if I want access to it anywhere in
>my program.
>--
>Kathleen MacMahon (s...@together.net)
>http://www.rahul.net/galen/index.html

Other Threads