Solved

Creating TStringLists

Posted on 1998-04-01
9
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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