The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,861 other subscribers

Why you should try to avoid using Variant operations in Delphi

Posted by jpluimers on 2018/04/17

[WayBackDelphi “WAT?!” of the day: var v1, v2: Variant; begin v1 := True; v2 := True… shows some interesting code.

The [WayBackVariant Types (Delphi) – RAD Studio: Variant Type Conversions (and the decimal point/comma handling or rounding issues not documented there) usually kill you and are the most important reason to avoid Variant operations in Delphi.

–jeroen


// Delphi "WAT?!" of the day:
var
v1, v2: Variant;
begin
v1 := True;
v2 := True;
Writeln(v1 + v2); // -2
Readln;
end.

5 Responses to “Why you should try to avoid using Variant operations in Delphi”

  1. mesignapk's avatar

    mesignapk said

    I wanna learn Coding. From where I should start?? I am beginner and currently running a blog https://geekolive.com

  2. Arnaud Bouchez's avatar

    Arnaud Bouchez said

    In fact, it is as expected. For variants, boolean are mapped as a COM Boolean, i.e. WordBool and not a pascal/Delphi boolean, with 0 or $ffff as values.
    So, with sign extension, the result is as expected.
    And note that FPC will give the very same results (at least in Delphi compatibility mode).

    • jpluimers's avatar

      jpluimers said

      Indeed. In the Variant documentation, COM is only mentioned once and OleVeriant is only at the end. It should be stressed that the stock conversions are all involving COM data types which can behave quite differently from Delphi built-in types.

      • Arnaud Bouchez's avatar

        Arnaud Bouchez said

        To be accurate, WordBool is a true Delphi built-in type, just as native than Boolean, but in practice only used with COM/variants… :)

Leave a reply to mesignapk Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.