How to cast one interface to another in Delphi 7?

Viewed 628

I am stuck with the following problem:

I have a class, which was generated from an xsd file using the Delphi 7 XML Data binding wizard (New -> Other -> XML Databindng).

I need to find a way to add methods to the generated code:

IXMLGlobeSettingsType = interface(IXMLNode)
    ['{9A8F5C55-F593-4C70-85D2-68FB97ABA467}']
    { Property Accessors }
    function Get_General: IXMLGeneralSettingsType;
    function Get_Projector: IXMLProjectorSettingsType;
    function Get_LineMode: IXMLLineDrawingSettingsType;

    { Methods & Properties }
    property General: IXMLGeneralSettingsType read Get_General;
    property Projector: IXMLProjectorSettingsType read Get_Projector;
    property LineMode: IXMLLineDrawingSettingsType read Get_LineMode;

    //procedure SetDefault;   {To be added}
  end;

The interface is implemented by a corresponding class, which is also generated by the wizard:

TXMLGlobeSettingsType = class(TXMLNode, IXMLGlobeSettingsType)
  protected
    { IXMLGlobeSettingsType }
    function Get_General: IXMLGeneralSettingsType;
    function Get_Projector: IXMLProjectorSettingsType;
    function Get_LineMode: IXMLLineDrawingSettingsType;
  public
    procedure AfterConstruction; override;
  end;

And in order to define my own extensions to the generated code, I have defined the following interface:

IDefaultable = interface
   procedure SetDefault;
end;

With the following implementation class:

DefaultableXMLGlobeSettingsType = class(TXMLGlobeSettingsType, IDefaultable)
  public
    procedure SetDefault;
  end;

However, I just realized that Delphi 7 does not let me cast one interface to another (or even from an interface to an object). So the following code will raise an error:

defaultSettings : IDefaultable;    
FGlobeSettingsIntf: IXMLGlobeSettingsType; // FGlobeSettingsIntf is in fact a DefaultableXMLGlobeSettingsType

// some code

defaultSettings := FGlobeSettingsIntf as IDefaultable; // error: operator not applicable to this operand type

I am pretty much stuck here. How can get around this error? Is there a way (even an ugly one) in Delphi 7 to cast the Interface to an object and then back to another interface.

2 Answers
Related