Solved

Runtime creation of components (and tracking, using, and freeing them)

Posted on 2001-06-08
7
198 Views
Last Modified: 2010-04-06
Okay, basicly I have a program that uses XML to store information about user set options.  What I want to do is use TStringLists to track all the options.  To do this the a section would be stored in StringListX and all subsections would be associate like so

StringListX.AddObject(SectionName,SubSectionStringList)

Well what I get into is I need to create a unknown number of stringlists to accomidate any number sections the user might create.

My problem is this, I'm not all that firmiliar with runtime creation of Objects, Persistants, whatever.  

What I would like to see is an example of how to a create a variable number of TStringLists during runtime and how I would go about accessing, and freeing them.
0
Comment
Question by:felonious
  • 3
  • 3
7 Comments
 
LVL 2

Accepted Solution

by:
peymanz earned 300 total points
Comment Utility
You can use a TList to store your TStringLists. Something like this:

MyLists := TList.Create;

// this function creates a new string list and adds
// it to MyLists
function CreateNewStringList: integer;
begin
  Result := MyLists.Add(TStringList.Create)
end;

to retrive a string you need to have its index:

// this function retrives the desired string list given
// its index. The function throws an exception if the
// index is out of range
function GetStringList(const index: integer):TStringList;
begin
  Result := TStringList(MyLists[index]);
end;

// To free all the string lists you have to free them
// individually
procedure FreeStringList;
begin
  while (MyLists.Count > 0) do begin
    GetStringList[0].Free;
  end;
  // the following line free the mylists object
  mylists.Free;
end;

offcourse you can put all this in a class

Feel free to ask question/comments if needed

regards
// peymanz
0
 
LVL 8

Expert Comment

by:TOndrej
Comment Utility
or use TObjectList which can Free owned elements automatically
0
 

Author Comment

by:felonious
Comment Utility
Wow, you guys are quick.  I have a small problem though and I guess a bit of code will illustrate:

var
    I : Integer;
    TempList : TStringList;

begin
    TempList := TStringList.Create;

    MainForm.LoadSectionList(TempList);
    For I := 0 to TempList.Count-1 do
    begin
         InfoItems.AddObject(TempList[I],TStringList.create);
         if SectionExist('Events') then
         begin
             LoadSubSectionList(TStringList(InfoItems.Objects[I]));
         end
         else LoadKeyList(TStringList(InfoItems.Objects[I]));
         InfoObjects.Add(InfoItems.Objects[I]);
    end;
end;

and that routine is called for as many different sections and subsections as I need to.  I create the Object and String lists in Form.Create and Free the in Form.Destroy.  This all works great, but maybe you spotted my problem?  peymanz gave me the bulk of the answer I was looking for, but I also used a TObjectList to manager the TStringList as TOndrei suggested.  How do I award these points?  Let me know what you all think.  

P.S.  Am I forgetting anything in the code?  I initially wanted to use a try statement around the .creates but... let me know if i should please :)
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Expert Comment

by:peymanz
Comment Utility
I see no harm in putting the creates in try statements. It only raises the quality of your code. The only thing that might go wrong are that maybe the memory runs out, which is possible if you have a huge number of Keys and subsections.

ps. "maybe you spotted my problem?" Is that the point problem or a problem in code.

You divide the points as you see fit.

This was not a hard question, so you could have offered less points.

regards
peymanz
0
 

Author Comment

by:felonious
Comment Utility
Yes I could have offered more points but I wanted aa quick turn around...
0
 

Author Comment

by:felonious
Comment Utility
umm... less points i mean.. eh he
0
 
LVL 2

Expert Comment

by:peymanz
Comment Utility
Glad I could help, and thanks for the points.

regards
// peymanz
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

15 Experts available now in Live!

Get 1:1 Help Now