Solved

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

Posted on 2001-06-08
7
199 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

18 Experts available now in Live!

Get 1:1 Help Now