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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:51:27+00:00 2026-06-18T00:51:27+00:00

I am having a strange problem of using interface in different versions of Delphi.

  • 0

I am having a strange problem of using interface in different versions of Delphi. The following minimized code compiles and runs as expected in Delphi XE and higher but not in Delphi 7. Specificaly, it seems when compiling in Delphi 7, the function TForm1.Load: IMoleculeSubject; does not returns the correct result, i.e., the correct reference to the newly created instance. Could you help to comment about the reason and possible workaround? Many thanks!

uInterface.pas

    unit uInterface;
    
    interface
    
    type
    
      IMoleculeSubject = interface
      ['{BEB4425A-186C-45DF-9DCE-C7175DB0CA90}']
      end;
    
      TMoleculeSubject = class(TInterfacedObject, IMoleculeSubject)
      end;
    
    implementation
    
    end.
    

uBusiness.pas

    unit uBusiness;
    
    interface
    
    uses
      uInterface;
    
    type
    
      TMoleculeDecorator = class(TMoleculeSubject) 
      private
        FID: Integer;
      public           
        property ID: Integer read FID;
        constructor Create;
      end;
    
    implementation
    
    { TMoleculeDecorator }
    
    constructor TMoleculeDecorator.Create;
    begin
      inherited Create;
    
      FID := Random(100);
    end;
    
    end.

Unit1.pas

    unit Unit1;
    
    interface
    
    uses
      uInterface, uBusiness,
    
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, 
      Forms, Dialogs;
    
    type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        function Load: IMoleculeSubject;
      public
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      MolSubject: IMoleculeSubject;
    begin
      MolSubject := Load;
    
              // The down-cast is to show the returned result is wrong in Delphi 7!
      Caption := IntToStr(TMoleculeDecorator(MolSubject).ID);
    end;
    
    function TForm1.Load: IMoleculeSubject;   
    var
      MolSubject: IMoleculeSubject;
    begin
      MolSubject := TMoleculeDecorator.Create;
      Result := MolSubject;
    end;
    
    end.
    
    
  • 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-06-18T00:51:28+00:00Added an answer on June 18, 2026 at 12:51 am

    The Load function works perfectly well in all versions of Delphi. The problem is your cast, which is what is known as an unsafe typecast. An unsafe typecast from an interface reference to an object has ill-defined behaviour in older versions of Delphi. However, the behaviour is well-defined in modern Delphi. The documentation says more.

    So, the basic problem is that your expectations for the behaviour are not compatible with the Delphi 7 version of the language.

    If you get the interface to return the ID you will find that the interface you are creating is as expected.

    program InterfaceDemo;
    
    {$APPTYPE CONSOLE}
    
    uses
      Classes;
    
    type
      IMyIntf = interface
        function GetID: Integer;
      end;
    
      TImplementingObject = class(TInterfacedObject, IMyIntf)
      private
        FID: Integer;
        function GetID: Integer;
      public
        constructor Create;
      end;
    
    { TImplementingObject }
    
    constructor TImplementingObject.Create;
    begin
      FID := Random(100);
      Writeln(FID);
    end;
    
    function TImplementingObject.GetID: Integer;
    begin
      Result := FID;
    end;
    
    var
      MyIntf: IMyIntf;
    
    begin
      Randomize;
      MyIntf := TImplementingObject.Create;
      Writeln(MyIntf.GetID);
      Readln;
    end.
    

    It’s rather unusual to ask for the implementing object from an interface. To do so suggests that there is a problem with your design. Should you really need to do so there are a few options:

    • In modern Delphi you can use the type-safe case with the as operator.
    • In older Delphi versions there are hacks that retrieve the implementing object: Casting a delphi interface to its implementation class without modifying the interface
    • You could add a function to the interface that returns the implementing object.

    The latter option works in all versions of Delphi and does so without resorting to subterfuge.

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

Sidebar

Related Questions

I'm having a strange problem here. Here's the code, I'm using to fetch a
I'm having a strange problem with Zend Framework under Ubuntu 11.10. I'm using a
I'm having a very strange problem. I'm using dfs-datastores Pail abstraction to write data
I'm having a strange problem. I have a HTML page with PHP code which
I'm having a strange problem using PHP's OAuth PECL library - I seem to
I am having a very strange problem - I am using more or less
I'm having a real strange problem using GCC for ARM with the optimizations turned
I'm having a strange problem while using NSDictionnary ... I'm trying to retrieve an
I'm having this strange problem with cloned elements(using .clone(true)) with draggable and resizable functionalities
I am having a strange problem when using this XML-to-NSDictionary class.really it was working

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.