Board index » delphi » Strange behaviour of variant to single

Strange behaviour of variant to single


2006-05-20 01:46:41 AM
delphi94
Hi dear reader
I have a strange problem which I can not resolve by myself:
A function returning a Variant value stores it is return value into a
variable of type Single:
Function GetValue(Params):Variant;
Begin
... Use Params to locate a table and Field in an Access database
... Determine the field type and retrieve the field-value: Result :=
Table.FieldByName(..).AsString, or .AsFloat etc.
End;
In another procedure I use:
Var
LSize,
LSize1,
LSize2 : Single;
LSize1 := GetValue(Tablename1, Fieldname1);
LSize2 := GetValue(Tablename1, Fieldname2);
...
LSize := GetValue(Tablename2, Fieldname3);
LSize1 and 2 are filled correctly, whilest LSize always remains 0, although
GetValue really returned a value.
Changing the name of variable LSize into for example XSize has no effect.
Changing the place of the LSize declaration in the Var-Section doesn't help
either.
When I change the code to
XVariant := GetValue(Tablename2, Fieldname3);
LSize := XVariant;
I can see that XVariant is filled with the proper value. LSize itself still
does not! Even worse: In this code the second line is not even compiled.
Viewing the code in the CPU-Window I noticed that for LSize1 and 2 the call
to GetValue is followed by a call to a VariantManager. For the LSize after
the call to GetValue is followed only by a single fst(0) statement .
What can I do to make sure that LSize will be properly filled??
And ...
I have used this construction a lot of times in my progam. At this point I
do not trust them anymore.
By the way, I am using Delphi 6 and connecting to an Access/SQL databse
through an ADO connection.
Thanks
E. Vrije
 
 

Re:Strange behaviour of variant to single

Quote
LSize1 and 2 are filled correctly, whilest LSize always remains 0,
although GetValue really returned a value. Changing the name of
variable LSize into for example XSize has no effect. Changing the
place of the LSize declaration in the Var-Section doesn't help either.
Viewing the code in the CPU-Window I noticed that for LSize1 and 2
the call to GetValue is followed by a call to a VariantManager. For
the LSize after the call to GetValue is followed only by a single
fst(0) statement .

What can I do to make sure that LSize will be properly filled??
You probably got this information from the IDE while debugging?
Optimization makes the values displayed by tooltips in the IDE while
debugging wrong sometimes. You shouldn't trust those values. If you get
this sort of behaviour you should use message boxes instead to display
the value:
ShowMessage(FloatToStr(LSize));
The line not being compiled is also caused by the optimizer: useless
assignments are optimized out. An assignment is useless if you never
read the variable after the assignment, or if there's another
assignment before it is read. This will also give you a hint about the
assignment not being used. Try and also program code that is free of
hints and warnings.
--
The Fastcode Project: www.fastcodeproject.org/
 

Re:Strange behaviour of variant to single

"Eef Vrije" <XXXX@XXXXX.COM>wrote
Quote
...
Function GetValue(Params):Variant;
Begin
...
Result :=
Table.FieldByName(..).AsString, or .AsFloat etc.
End;
In another procedure I use:
Var LSize, LSize1, LSize2: Single;
...
LSize1 := GetValue(Tablename1, Fieldname1);
LSize2 := GetValue(Tablename1, Fieldname2);
...
LSize := GetValue(Tablename2, Fieldname3);
...
I have used this construction a lot of times in my program.
At this point I do not trust them anymore.
Eef,
Why do you convert all your data through a *variant*?
That seems a waste of CPU time and a potential source
of problems -- as you have discovered.
Regards, JohnH