bejo
bejo

Reputation: 43

Need a name=value class similar to TStringList but the value part is variant

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.

Upvotes: 0

Views: 1957

Answers (4)

inzKulozik
inzKulozik

Reputation: 114

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);

Upvotes: 1

Uwe Raabe
Uwe Raabe

Reputation: 47758

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

Upvotes: 3

Mason Wheeler
Mason Wheeler

Reputation: 84590

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.)

Upvotes: 3

mghie
mghie

Reputation: 32334

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>.

Upvotes: 4

Related Questions