Search code examples
delphidelphi-2007

store variant value in object property of TStringList


i need store a variant value in a TStringList, so far i try this

var
 list : TStringList;
 v   : OleVariant;

  List..AddObject('Item1',v);

or

  List..AddObject('Item1',TObject(v));

but in both cases the code not compile because the typecast is invalid or the types are incompatibles.

so the question is, how i can store a variant value in a TStringlist ?


Solution

  • I think the only way would be to wrap your variant in a class and put a reference to that in the object of the stringlist item. You would of course need to take care that the wrapper instances are properly freed when freeing the stringlist. Using the recent Delphi version's that would easily be done by using the "OwnsObjects" parameter of the stringlist.