Solved

Creating TStringLists

Posted on 1998-04-01
9
425 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

624 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