BigONotation
BigONotation

Reputation: 4538

How to cast one interface to another in Delphi 7?

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.

Upvotes: 2

Views: 862

Answers (2)

David Heffernan
David Heffernan

Reputation: 613461

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

This error indicates that the definition of IDefaultable does not include a GUID. Without a GUID it is not possible to query for an interface, which is what the as operator does in this context.

The as operator, when used with a interface on the right hand side, is implemented by a call to IInterface.QueryInterface. That requires a GUID to be associated with the interface.

Resolve the problem by adding a GUID when you declare IDefaultable.

Upvotes: 6

Uwe Raabe
Uwe Raabe

Reputation: 47819

This is what Supports is for:

if Supports(FGlobeSettingsIntf, IDefaultable, defaultSettings) then begin
  defaultSettings.SetDefault;
end;

Upvotes: 3

Related Questions