Board index » delphi » Stringlists begetting stringlists ??

Stringlists begetting stringlists ??

Hello All,

What is the best way to have an item in a stringlist have another
stringlist{*word*154} off it.

For example-

SL1 - holds peoples names.
SL(food) - holds the favourite food of every person.

So SL1[0] - is 'Bob', and Bob's favourite food (pizza, beer, chips) gets
stored in SLbobsfood.

The thing is - each person may, or may not want to store their favourite
food, so some may have no need for a Food stringlist at all, some may
have 2 items in there, and some may have 50.

The program will need to create the Food stringlist and associate it
with SL1[0] (Bob's place on the first stringlist) for every person in
SL1.

I know I can associate objects with stringlist entries (but I don't know
how, can't find any examples), and D6 has 'bucketlist', which might be
able to help me - but again but I don't know how to use it and can't
find any examples. Delphi help in these areas tells you what tyhese do
rather than how to do it yourself.

Sorry for being so thick - can anyone help me?

Thanks for any help or suggestions,

Liam Healy

 

Re:Stringlists begetting stringlists ??


Quote
"L. Healy" <L.He...@occpsy.demon.co.uk> wrote in message
> What is the best way to have an item in a stringlist have another
> stringlist{*word*154} off it.
> I know I can associate objects with stringlist entries (but I don't know
> how, can't find any examples), and D6 has 'bucketlist', which might be
> able to help me - but again but I don't know how to use it and can't
> find any examples. Delphi help in these areas tells you what tyhese do
> rather than how to do it yourself.

ix := peopleList.AddObject ('Bob', tStringList.Create);
with tStringList (peopleList.Objects [ix]) do
    begin
    Add ('Pizza');
    Add ('Beer');
    Add ('Chips');
    end;

You must remember to free these sub-lists before deleting an entry in or
destroying the main list.

    tStringList (peopleList.Objects [ix]).Free;
    peopleList.Delete (ix);

This will work well if you just need one sub-list. If you need multiple
lists, then there are a couple of ways  to handle it. One would be to
continue using a single sub-list, but compress the entries for a sub-list
into one entry. So, for example, food could always be stored as entry zero
of the sub-list in the form <food>|...|<food>| ("Pizza|Beer|Chips|").
Another approach would be to define a class than could hold the various
lists required and use instances of that class instead of a string list,
e.g.

Type
tPeopleAttributes = class (tObject)
    private
        fFoods,
        fHobbies : tStringList;
    . . .
    end;

peopleList.AddObject ('Bob', tPeopleAttributes.Create);

Re:Stringlists begetting stringlists ??


Quote
"L. Healy" <L.He...@occpsy.demon.co.uk> wrote in message <news:o$eJZJAEKh98Ew2x@occpsy.demon.co.uk>...
> Hello All,

> What is the best way to have an item in a stringlist have another
> stringlist{*word*154} off it.

> I know I can associate objects with stringlist entries (but I don't know
> how, can't find any examples), and D6 has 'bucketlist', which might be
> able to help me - but again but I don't know how to use it and can't
> find any examples. Delphi help in these areas tells you what tyhese do
> rather than how to do it yourself.

Liam,

  SL.AddObject('Bob',TStringList.Create);
  (SL.Objects[0] as TStringList).Add('Bananas');
  (SL.Objects[0] as TStringList).Add('Apples');
  ...
  // when you're ready to free the string list...
  for i := 0 to SL.Count-1 do
    SL.Objects[0].Free;
  SL.Free;

Hope that helps.

Other Threads