Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating TStringLists

Posted on 1998-04-01
9
Medium Priority
?
433 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 40 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
Industry Leaders: 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

688 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