tags:

views:

455

answers:

4

I need a class similar to TStringList that could manage name & value pairs, but the value part is variant. Or perhaps it has a property like TStringList.Object but holds variants instead of objects.

Could anyone please point me to a free or open source implementation? I use Delphi 7.

Thank you.

+4  A: 

You have not given the Delphi version this is intended to be used with, but starting with Delphi 2009 you can use a TDictionary<string, Variant>.

mghie
+3  A: 

If you have Delphi 2009 or 2010, you can use the TStringList<T> class in DeHL to create a TStringList<Variant>. (You could also use TDictionary, but TStringList has a lot of extra functionality that you might not want to lose.)

Mason Wheeler
+3  A: 

You can derive from TStringList and use the Objects property to hold a wrapper object for a variant.

Uwe Raabe
+1  A: 
PVariantRec = ^TVariantRec;
TVariantRec = record
  Value : Variant;
end;

var
  lItem : PVariantRec;
  lMyStringList : TStringList;

lMyStringList := TStringList.Create;
lMyStringList.Sorted := true;
lMyStringList.OwnObjects := false;

//add
New(lItem);
lItem.Value := 'zzz';
lMyStringList.Add('name', TObject(lItem));

//remove
lItem := PVariantRec( lMyStringList.Objects[0] );
Dispose(lItem);
lMyStringList.Delete(0);
inzKulozik
Is it safe to do TObject(lItem) ?
bejo
Thats why I recommend a TObject to wrap the Variant...
Uwe Raabe
Yes, its safe. http://stackoverflow.com/questions/367130/i-want-to-assign-a-record-to-tstringlist-objects
inzKulozik