Variant Currency Bug?

Casting to fit truncates

And this is insane going on silently:

Placing 6.77 into an object in VB.NET (or indeed in .NET generally) results in it being a floating point value that is “boxed”.

There is no true concept of a variant in .NET. It is still possible to use reflection or the VB “is” or “isnot” operators to determine the actual type that is “boxed” by the object type. Or you can just cast it to double implicitly or explicitly if you know that’s what it is – via DirectCast() in VB.NET, or even one of the legacy Cxxx() functions such as CDbl().

Even in VBScript, which has something called a Variant, it still retains knowledge of what numeric type was assigned to it and will treat it as that type when you do math against it. You are just trading strict / early bound typing and its efficiencies / safeties for flexibility.

In Xojo, near as I can tell, a variant has a bunch of methods that will via a “best effort” convert whatever is in there to the type returned by that method. Things can get lost in the translation. My guess is that it was crafted with database fields in mind, and less thought was put into inferring the best type from a numeric constant.

It might be better to do something like var d as double = 6.77 and assign THAT to the variant; or possibly var v as Variant = String.ToDouble(“6.77”). But TBH I would go out of my way to avoid Variant in the first place, if humanly possible.

That will not fix the problem if we multiply the variant by an Integer.

The documentation says:

Variants used in an expression convert themselves to the type of the other operand, no matter what kind of data they contain, instead of deferring the type-conversion until runtime. This only affects expressions involving a Variant operand and an operand of some other type, not expressions in which both operands are variants.

Following the documentation the following code examples should result in 9 because there is no integer operand:

Var dotnine As Variant = 0.99
Var i As Integer = dotnine * 10.
Var dotnine As Variant = 0.99
Var i As Integer = dotnine * 10.0
Var dotnine As Variant = "0.99"
Var i As Integer = dotnine * 10.0
Var dotnine As Variant = 0.99
Var ten as Variant = 10
Var i As Integer = dotnine * ten
Var dotnine As Variant = "0.99"
Var ten as Variant = 10
Var i As Integer = dotnine * ten

I don’t like the unexpected behavior of changing the Variant to an Integer but I don’t see the behavior changing any time soon. I really don’t use Variants, so I prefer Xojo to fix/change other things. If I ever use Variants, I now know what to do to get the results I expect.

That’s basically the concept of a Variant. Holding any static value into a generic envelope. Interestingly an Object(Double(6.77)) * Integer(1000) = 6770 there and in Xojo it becomes wrongly Integer(Variant(Double(6.77))) * Integer(1000) = 6000, You know why? RTTI in expressions using dynamic content as Variants/Objects. No need to guess, by the way Xojo does not guess anything, it just apply dumb rules as “variant operator integer = Integer(variant) operator integer”.

What Xojo needs is runtime evaluation of dynamic content instead of Xojo’s bad fixed static conversion at compile time. Or
 a way to say Xojo “Don’t let me do it. Fire a compiler error in places subject to such wrong behavior. Don’t run until I workaround that part of the code using static types”.

It sucks to explain the same things over and over. That was already said.

Or you can just be explicit about what your intensions are and everything works as you want with no changes at all.

Sure.

Here it is again. Don’t worry if you don’t understand what I mean, they do.

Basically it means that if Xojo finds a Variant expression like 6.77 * 1000, it must do

6.77 * 1000 = 6770 , ALWAYS, or

If, as Xojo does it 6.77 * 1000 = 6000 as it currently do, It should rise a COMPILER error asking me to remove the variant and use a static type as Double, or whatever, in any way that the current static expression evaluator could do what is expected correctly.

No silent fails.

Thanks I fully understand an insult when I read one.

Me too. That’s a tie. :wink:

Hardly, I nearly suggested a method you could use to avoid your problems.

Dear, I have no problems.

I just just want to fix bugs. I want best practices. I want to protect the users. I do this because I care about Xojo. I care about the community. That has nothing to do with me. Please, stop being recurrently
 as you are. If you read, and understand, everything that I wrote, you wont be saying
 the things you say.

Please, if you still have nothing proper to add to conversation, just refrain your instincts of writing just for writing.

No hard feelings, I swear.

Editing your text to remove insults and then saying you don’t insult
 not good. I’ll say no more on the topic.

I replied insults, but can’t edit, seem like I can delete. If you agree we both could REMOVE this conversation starting from this very bad assumption :

to this point. You start.