[fpc-pascal] Re: Variant vs Pointer

Ludo Brands ludo.brands at free.fr
Sat Mar 3 15:52:17 CET 2012

> Think in my class like a Business Object.
> The struct is very similar with TParams it has many TParam. The data,
> in a TParam, is save in a Variant type.
> I need something like:
> u := TmyBO.Create;  // the name does matter
> u.Attr['name'].Value := 'Marcos';  // an instance is created and the
> name is 'name' and the type is string
> The attributes are created on demand and the data is saved into a
> Variant, like TParam.
> If I do not use a Variant, I need to create one class to each type
> that I will use, right?

Or define an internal storage format and convert the different types to that
format. That is also what variants are doing. The difference is that
variants have pre-defined assignment routines and conversions between the
different variant types which don't always fit what you want (date formats,
decimal points, precision, characters set, etc.). Assign fe. 1.2 to a
variant and it will store it as a double while you perhaps meant a 32 digit
precision numeric value.   

> If Variant is so bad, why TParam is implemented using it?

That is a very good question. Delphi compatibility is probably the answer. 

Just look at the VarFmtBCD custom variant format created to fit the higher
precision numeric data types and all the difficulties to get it right. 


More information about the fpc-pascal mailing list