?
Solved

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

Posted on 2001-06-08
7
Medium Priority
?
206 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
[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
7 Comments
 
LVL 2

Accepted Solution

by:
peymanz earned 1200 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
Technology Partners: 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!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
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, 15 hours left to enroll

762 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