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

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


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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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;
  Result := MyLists.Add(TStringList.Create)

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;
  Result := TStringList(MyLists[index]);

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

offcourse you can put all this in a class

Feel free to ask question/comments if needed

// peymanz

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
or use TObjectList which can Free owned elements automatically
feloniousAuthor Commented:
Wow, you guys are quick.  I have a small problem though and I guess a bit of code will illustrate:

    I : Integer;
    TempList : TStringList;

    TempList := TStringList.Create;

    For I := 0 to TempList.Count-1 do
         if SectionExist('Events') then
         else LoadKeyList(TStringList(InfoItems.Objects[I]));

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 :)
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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.

feloniousAuthor Commented:
Yes I could have offered more points but I wanted aa quick turn around...
feloniousAuthor Commented:
umm... less points i mean.. eh he
Glad I could help, and thanks for the points.

// peymanz
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.