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 4053722
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:29:00+00:00 2026-05-20T14:29:00+00:00

In Delphi I want to determine whether a particular OleVariant can be cast to

  • 0

In Delphi I want to determine whether a particular OleVariant can be cast to a particular data type without raising an exception if it can’t. Exceptions are not for program flow, right?

What I want is something like this, where Type could be anything supported by an OleVariant:

if TryVarAsType(variant, value) then ...

What I don’t want is

try
  value := Type(variant);
  // case where the variant could be converted to a Type
except
  // case where the variant could not be converted to a Type
end;

The case where the variant could not be converted to a Boolean is just a normal case that occurs regularly and doesn’t indicate any kind of error.

  • 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-20T14:29:01+00:00Added an answer on May 20, 2026 at 2:29 pm

    you can construct such function using the VariantChangeTypeEx function.

    uses
      VarUtils,
      Variants;
    
    function TryVarAsType( AVariant : OleVariant; const AVarType: TVarType ) :Boolean;
    var
       SourceType: TVarType;
    begin
      SourceType:=TVarData(AVariant).VType;
      //the types are ole compatible
      if (AVarType and varTypeMask < varInt64) and (SourceType and varTypeMask < varInt64) then
        Result:=
        (SourceType=AVarType) or
        (VariantChangeTypeEx(TVarData(AVariant), TVarData(AVariant), VAR_LOCALE_USER_DEFAULT, 0, AVarType)=VAR_OK)
      else
      Result:=False; //Here you must process the variant pascal types like varString
    end;
    

    and use like this

    TryVarAsType('1',varInteger);
    TryVarAsType('s',varInteger)
    

    this will work with only with ole compatible Variant types

      varEmpty    = $0000; { vt_empty        0 }
      varNull     = $0001; { vt_null         1 }
      varSmallint = $0002; { vt_i2           2 }
      varInteger  = $0003; { vt_i4           3 }
      varSingle   = $0004; { vt_r4           4 }
      varDouble   = $0005; { vt_r8           5 }
      varCurrency = $0006; { vt_cy           6 }
      varDate     = $0007; { vt_date         7 }
      varOleStr   = $0008; { vt_bstr         8 }
      varDispatch = $0009; { vt_dispatch     9 }
      varError    = $000A; { vt_error       10 }
      varBoolean  = $000B; { vt_bool        11 }
      varVariant  = $000C; { vt_variant     12 }
      varUnknown  = $000D; { vt_unknown     13 }
      varShortInt = $0010; { vt_i1          16 }
      varByte     = $0011; { vt_ui1         17 }
      varWord     = $0012; { vt_ui2         18 }
      varLongWord = $0013; { vt_ui4         19 }
      varInt64    = $0014; { vt_i8          20 }
    

    for the another types (pascal variants) like varString, varAny you must check the source and destination TVarType and write your own test cases.

    UPDATE

    As @David point me out, the locale settings can produce different results for the same values, so you must consider this answer just as initial step or tip to construct your own function and you must aware of locale settings issues caused in the proposed function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know how I can develop GUI applications(32 Bits) without using Delphi
I want to monitor the copy file function using Delphi. I can do it
In my Delphi development, I want to pass an array of const(that can contains
I want to develop a delphi application with an HTML/CSS graphical user interface, not
I want to know whether it is possible in Delphi to read a CD
If you have a Delphi project and want to add a type library to
I`m using Delphi 2009 and want to operate some XML data. I heard that
HI, Im using Delphi and I want to make an application that can do
In my Delphi Project i want to have a 'Settings' button that when clicked,
I want to write in Delphi (2009 - so I have generic dictionary class)

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.