Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert a variant to an integer or float.

1,130 views
Skip to first unread message

D-Fan

unread,
Jun 19, 2006, 6:06:54 PM6/19/06
to
What is the best way to convert a variant to an integer or a float?
There is no VarToFloat or VarToInt.

Thanks

Marc Rohloff [TeamB]

unread,
Jun 19, 2006, 7:07:05 PM6/19/06
to
On Mon, 19 Jun 2006 17:06:54 -0500, D-Fan wrote:

> What is the best way to convert a variant to an integer or a float?
> There is no VarToFloat or VarToInt.

Just copy it into a float or integer variable.

--
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com

Remy Lebeau (TeamB)

unread,
Jun 19, 2006, 7:11:06 PM6/19/06
to

"D-Fan" <D-...@TheAntiSpam.com> wrote in message
news:44971FFE...@TheAntiSpam.com...

> What is the best way to convert a variant to an integer
> or a float? There is no VarToFloat or VarToInt.

Variant has various conversion operators implemented. Just assign it
directly, ie:

Variant v1 = 12345;
int i = v1;

v = 12345.0;
float f = v;


Gambit


D-Fan

unread,
Jun 19, 2006, 8:36:57 PM6/19/06
to
Ok. I have done this but I wasn't sure that it is best practice. There
are functions for VarToStr, etc. Sometimes I use pass variants into
routines where the type is of integer and it works, but I find it safest
to assign the variant to an integer variable and pass the new variable
to the funtion. More specifically I am speaking of situations where I
am passing a tfield as in " ds.fieldbyname('var').value " as an integer
value parameter. This is not a stable practice even though the value is
a number at the time of the operation. It seems as though you are safer
passing the field value to a variable prior to passing to the function.

Thanks.

Remy Lebeau (TeamB)

unread,
Jun 19, 2006, 9:23:58 PM6/19/06
to

"D-Fan" <D-...@TheAntiSpam.com> wrote in message
news:44974329...@TheAntiSpam.com...

> There are functions for VarToStr, etc.

They are used by the VCL internally.

> Sometimes I use pass variants into routines where the type is
> of integer and it works, but I find it safest to assign the variant
> to an integer variable and pass the new variable to the funtion.

If the function takes an 'int' as a parameter, then there is no need to use
a separate variable. You can pass the Variant to the parameter directly,
and it will convert the same way as it does being assigned to a variable.

> More specifically I am speaking of situations where I am passing a
> tfield as in " ds.fieldbyname('var').value " as an integer value
parameter.

TField has its own conversion properties, such as AsInteger, ie:.

function(ds.FieldByName('var').AsInteger);

> This is not a stable practice even though the value is a number at the
> time of the operation.

Why do you think it is unstable?

> It seems as though you are safer passing the field value to a variable
> prior to passing to the function.

There is no difference between doing that and passing the Variant to the
parameter directly.


Gambit


John Herbster

unread,
Jun 20, 2006, 9:38:15 AM6/20/06
to

"D-Fan" <D-...@TheAntiSpam.com> wrote

> Ok. I have done this but I wasn't sure that it is best practice.

D,
I think it is good practice to avoid the extra overhead of
variants, wherever possible.
--JohnH


Mike Shkolnik

unread,
Jun 20, 2006, 1:50:40 PM6/20/06
to
You may use the VarAsType function:
dbl := VarAsType(v, varDouble)
i := VarAsType(v, varInteger)
--
With best regards, Mike Shkolnik
E-mail: mshk...@scalabium.com
WEB: http://www.scalabium.com

"D-Fan" <D-...@TheAntiSpam.com> wrote in message

news:44971FFE...@TheAntiSpam.com...

Remy Lebeau (TeamB)

unread,
Jun 20, 2006, 5:15:01 PM6/20/06
to

"Mike Shkolnik" <mshkol...@ukr.net> wrote in message
news:4498...@newsgroups.borland.com...

> You may use the VarAsType function:
> dbl := VarAsType(v, varDouble)
> i := VarAsType(v, varInteger)

VarAsType() returns a new Variant. The conversion operators of Variant
already perform internal casting similar to VarAsType(), so there is no need
to make more temporary copies than are already being used implicitally.


Gambit


0 new messages