[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2001-06-08
7
Medium Priority
?
209 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

656 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