Solved

Creating TStringLists

Posted on 1998-04-01
9
418 Views
Last Modified: 2008-02-26
Hi,
    I'm trying to dynamically create a number of TStringLists stored in an array. When I try to do this, I get an EGPFault. I don't know why.
The array is declared :

columns : array[1..5] of TStringList;

They are created thus :

for z := 1 to cols do
         columns[z].Create;

where cols is just an int between 1 and 10.
It gives the error when z is 1, so I'm stumped.
Can anyone help?

Cheers, Nik
0
Comment
Question by:PiersBull
  • 3
  • 3
  • 2
  • +1
9 Comments
 

Expert Comment

by:bijoyn
ID: 1361166
Which version of Delphi are u using ?.
0
 

Author Comment

by:PiersBull
ID: 1361167
Hi bijoyn,
   I'm on Delphi 1.
Cheers

0
 
LVL 5

Accepted Solution

by:
inter earned 10 total points
ID: 1361168
Hi there,

you should create the objects as:

for z := 1 to 5 do
     columns[z] := TStringList.Create;

WHY?
because TStrinList is a CLASS. When you create one that is called INSTANCE which covers the actual code and data for it CLASS is just an abstract things. So in the correct creation above we tell CLASS TStringList to create and give us an INSTANCE for our use. I hope it be clear. So for ever CLASS you should create an INSTANCE with this logic.

Regards,
Igor
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Expert Comment

by:bijoyn
ID: 1361169
I tried your e.g. in Delphi 2 & 3 and it works absolutely fine. All I can say is change your 'For' loop to 'For z := 0 to 5' and try the same thing. I know this sounds stupid but I cannot think of anything else.
0
 

Expert Comment

by:bijoyn
ID: 1361170
Inter is absolutely right, How stupid can I get. Sorry Nik. Thanks Inter.
0
 

Author Comment

by:PiersBull
ID: 1361171
Hi again,
    That seems to have sorted the problem out, but now I can't free the memory! I've tried this :

for z := 0 to 5 do
columns[z] := TStringList.Free;

This gives an invalid method error. Any ideas?

Cheers, Nik

0
 
LVL 4

Expert Comment

by:d003303
ID: 1361172
Hi,
just try this code :

for z := 0 to 5 do
   columns[z].Free;

Now colums is instanciated and contains an instance, not a class pointer.

Slash/d003303
0
 
LVL 5

Expert Comment

by:inter
ID: 1361173
Hi,
as d003303 said, you should free the instance. Actually (in general) you only need to make a call such as TStringList.Create in creation.        IGOR
0
 

Author Comment

by:PiersBull
ID: 1361174
Cheers, guys!
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 242
When i run adoquery my application freezes 26 180
how can i search if string exist in array ? 3 63
update joined tables 2 55
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question