Solved

Creating TStringLists

Posted on 1998-04-01
9
407 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now