Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7619651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:44:27+00:00 2026-05-31T03:44:27+00:00

Variants are always fun, eh? I am working on a legacy application that was

  • 0

Variants are always fun, eh?

I am working on a legacy application that was last in D2007 to migrate it to Delphi XE.

Variants have changed quite a bit in the interim.

This line of code:

if (VarType(Value) = varString) and (Value = '') then 
  Exit;

returned True and exited in D2007, but doesn’t in Delphi XE.

I have changed it to this:

if VarIsStr(Value) and (VarToStr(Value) = '') then
    Exit;

I’m not convinced this is the “best” way to go. The Variants unit doesn’t have a specific call to do this, and I certainly recall this being an issue for folks in the past. However, a search revealed no library function or any other accepted way.

Is there a “correct” or better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:44:29+00:00Added an answer on May 31, 2026 at 3:44 am

    Updated: String-specific to avoid exceptions:

        if VarIsStr(Value) and (Length(VarToStr(v))=0) then ...
    

    Update3: If you want better performance and less string heap memory waste try this.
    Imagine that the strings are 64K in length. The code above does a VarToStr and allocates perhaps 64K of UnicodeString heap space to hold the data, just so we can just look for the nul terminator at the end of the string for BSTR, and for nil-pointers for other types.

    The code below is a slightly odd in that one does not commonly reach into the internal representation of variants, but David pointed out the bugs and I re-re-tested it and it seems to work, although no warranty is expressed or implied. A unit test for this puppy would be good. At some future date if Delphi RTL gods decided to rename the internal representation of the Variant structure fields, the code below would need to be changed.

    function VarStrEmpty(v:Variant):Boolean;
    var
      data:PVarData;
    begin
        data := FindVarData(V);
      case data^.VType of
         varOleStr:
                result := (data^.VOleStr^=#0);
         varString:
                result := (data^.VString=nil);
         varUString:
                result := (data^.VUString=nil);
         else
          result := false;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two columns in a table that will always be unique, vendor_identifier and
I'm using Delphi 2007. I need write a FormatDateTime function that always return 01/01/
I know variants of this question have been asked before (even by me), but
I have a C++ overloaded function and one of its variants receives a wstring
I have a simple but subtle question. Below you see two different declaration variants
I have a problem that can be simplified as follows: I have a particular
I noticed that, among OCaml programmers I know, some of them always use polymorphic
Using Delphi 7 I am reading from a serial port. The read is always
In my application (Delphi 2007) I want to drag items from a ListView to
I am currently working on a C-based, NDK-based, Android application. This application needs to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.