Link to home
Start Free TrialLog in
Avatar of jcglen
jcglen

asked on

StringList Objects

I am interested in using the Objects property of a StringList to associate objects with certain Lines of a RichEdit. However, Delphi's help says that certain StringList objects ignore the Object property because "they don't make sense, like Lines in a Memo". I'm not sure exactly what that means but is there a way to associate Objects with Lines in a memo (or richedit) object?
Avatar of jcglen
jcglen

ASKER

Edited text of question
ASKER CERTIFIED SOLUTION
Avatar of rwilson032697
rwilson032697

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello jcglen
   Associating line indexes with TList pointers is not a good idea in my opinion. I use the Objects member (or related Data member in TListItems) a lot because they are very useful. There are two ways to do what you are trying to do. First of all, since the Lines property of a TRichEdit is itself of type TStrings, you could associate your object with Lines.Objects. The second more complicated thing keeps your objects associated with your string list (which is what you want to do, right?).

First, declare a small class, called TLine:

  TLine = class
  private
    FLine : String;
  end;

Now, to associate a given line with your object, create an object of type TLine and associate it with your string list member.

  MyLine := TLine.Create;
  MyLine.FLine := RichEdit1.Lines[y];
  MyStringList.Objects[x] := MyLine;

Now you have kept the text in your string list and have an associated object. You can dereference the string like this:
 
  TLine(MyStringList.Objects[AnIndex]).FLine;

You can also search your stringlist using IndexOf, IndexOfObject, and so on (just like you can with Lines). The Objects and Data members of various classes in Delphi are very powerful in this regard.

Finally, in your destructor, you have to free the memory you have allocated for each of these objects. Do the following:

  for i := 0 to Pred(MyStringList.Count) do
    TLine(MyStringList.Objects[x]).Free;

Regards,
Edo

I am tired, there is a mistake in the destructor, [x] should read [i].