Solved

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

Posted on 2001-06-08
7
200 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
ID: 6169145
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
ID: 6169212
or use TObjectList which can Free owned elements automatically
0
 

Author Comment

by:felonious
ID: 6169357
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 2

Expert Comment

by:peymanz
ID: 6169548
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
ID: 6169557
Yes I could have offered more points but I wanted aa quick turn around...
0
 

Author Comment

by:felonious
ID: 6169559
umm... less points i mean.. eh he
0
 
LVL 2

Expert Comment

by:peymanz
ID: 6169607
Glad I could help, and thanks for the points.

regards
// peymanz
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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