I hate Variants, did I say that before? They just make everything more complex than necessary.

I hate Variants, did I say that before? They just make everything more complex than necessary.

Did you ever try to compare two variants? E.g. you have got one that contains an empty string (”) and another that contains a date. Now you want to know whether they are equal or not. Simple?

if v1 <> v2 then
    WriteLn('not equal')
else
    WriteLn('equal');

Not so. The code above raised a EVariantConversionError exception because it could not convert an empty string into a date. ...
http://blog.dummzeuch.de/2014/07/01/comparing-variants/

Comments

  1. I'm not using Variants, but is VarSameValue or VarCompareValue is not what you need?
    I should read the link first, then comment ..

    ReplyDelete
  2. VarSameValue will raise the exact same exception.

    You should write:
    if v1 <> VarToStr(v2) then

    Also the comparison itself might be just wrong because if v2 contains a date it will never equal to empty string (except when using VarToStr which returns an empty string when the variant is null). It rather might contain Null which you can check.

    ReplyDelete
  3. That's what I found out later and therefore changed the blog post.

    ReplyDelete
  4. My code tries to find out whether a variant's value changes due to an assignment, so a database table record needs to be written or not. So all I want to know is whether the variants are exactly the same or not.
    (Did I mention that I hate Variants?)

    ReplyDelete
  5. Hate often is the result of misunderstandings ;)

    ReplyDelete
  6. Poor misunderstood Variants, then?
    I wonder whether the hate is mutual... ;-)

    ReplyDelete
  7. Using variants require care. It may be a date, it may be an OLE object.

    ReplyDelete

Post a Comment