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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:23:40+00:00 2026-05-28T06:23:40+00:00

I have a Delphi generic class that exposes a function with an argument of

  • 0

I have a Delphi generic class that exposes a function with an argument of the generic type. Inside this function, I need to pass an instance of the generic type on to another object expecting a Variant type. Similar to this:

type
  IMyInterface = interface
    DoStuff(Value: Variant);
  end;      

  TMyClass<T> = class
    FMyIntf: IMyInterface
    procedure DoStuff(SomeValue: T);
  end;

[...]

procedure MyClass<T>.DoStuff(SomeValue: T);
begin
  FMyIntf.DoStuff((*convert SomeValue to Variant here*));
end;

I tried using Rtti.TValue.From(SomeValue).AsVariant. This worked for integral types, but blew up for Booleans. I don’t quite see why, since normally I’d be able to assign a Boolean value to a Variant…

Is there a better way to make this conversion? I only need it to work for simple built-in types (excluding enumerations and records)

  • 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-28T06:23:40+00:00Added an answer on May 28, 2026 at 6:23 am

    I think there is no direct way to convert generic type to variant because variant cannot hold all the possible types. You must write your specific conversion routine. E.g.:

    interface
    //...
    type
      TDemo = class
      public
        class function GetAsVariant<T>(const AValue: T): Variant;
      end;
    //...
    implementation
    uses
      Rtti,
      TypInfo;
    //...
    
    { TDemo}
    
    class function TDemo.GetAsVariant<T>(const AValue: T): Variant;
    var
      val: TValue;
      bRes: Boolean;
    begin
      val := TValue.From<T>(AValue);
      case val.Kind of
        tkInteger: Result := val.AsInteger;
        tkInt64: Result := val.AsInt64;
        tkEnumeration: 
        begin
          if val.TryAsType<Boolean>(bRes) then
            Result := bRes
          else
            Result := val.AsOrdinal;
        end;
        tkFloat: Result := val.AsExtended;
        tkString, tkChar, tkWChar, tkLString, tkWString, tkUString:
          Result := val.AsString;
        tkVariant: Result := val.AsVariant
        else
        begin
          raise Exception.Create('Unsupported type');
        end;
      end;
    end;
    

    Because TValue.AsVariant handles most of the type conversions internally, this function can be simplified. I will handle enumerations in case you could need them later:

    class function TDemo.GetAsVariant<T>(const AValue: T): Variant;
    var
      val: TValue;
    begin
      val := TValue.From<T>(AValue);
      case val.Kind of
        tkEnumeration:
        begin
          if val.TypeInfo = TypeInfo(Boolean) then
            Result := val.AsBoolean
          else
            Result := val.AsOrdinal;
        end
        else
        begin
          Result := val.AsVariant;
        end;
      end;
    

    Possible usage:

    var
      vValue: Variant;
    begin
      vValue := TDemo.GetAsVariant<Boolean>(True);
      Assert(vValue = True); //now vValue is a correct Boolean
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Delphi form inside a DLL (I know that this restricts the
I want to write in Delphi (2009 - so I have generic dictionary class)
I have Delphi 2007 code that looks like this: procedure WriteString(Stream: TFileStream; var SourceBuffer:
I have a Delphi application running under Windows that needs to block until another
I'm using Delphi 9's TDictionary generic class. My TDictionary looks like this: g_FileHandlers :
I need loging all HTTP request (from any application). I have Delphi 7.0. Anybody
I have a Delphi 7 application that has two views of a document (e.g.
I have a Delphi DLL with a function defined as: function SubmitJobStringList(joblist: tStringList; var
I have a delphi (Win32) web application that can run either as a CGI
I have a Delphi 5 executable that calls into a .NET assembly via the

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.