Class which implements interface via property requires interface type

It’s impossible to access a property/method of a class when the class implements the interface via a property and the instance variable is not of the type of the interface but the class itself.

See the following example, which fails to compile with:

Severity	Code	Description	Project	File	Line	Suppression State
Error		(E44) No member "Vgid" on type "TestTypeWrapper"	ImplementsTest	ImplementsTest\ImplementsTest\Program.pas	53	
Error		(E44) No member "Vgid" on type "TestTypeWrapper"	ImplementsTest	ImplementsTest\ImplementsTest\Program.pas	50	

Code:

namespace ImplementsTest;

interface

uses
  System.Linq;

type
  Program = class
  public
    class method Main(args: array of String): Int32;
  end;

implementation

type
  ITestType = interface
    property Vgid: String read write;
  end;

type
  TestType = class(ITestType)
  public
    property Vgid: String;
  end;

type
  TestTypeWrapper = class(ITestType)
  protected
    fTestType: ITestType;
    property TestType: ITestType read fTestType; implements ITestType;
  public
    constructor;
  end;

constructor TestTypeWrapper;
begin
  fTestType := new TestType;
end;

class method Program.Main(args: array of String): Int32;
begin
  var t1 := new TestType;
  t1.Vgid := "AAA";

  var t2: ITestType := new TestTypeWrapper;
  t2.Vgid := "AAA";

  var t3 := new TestTypeWrapper;
  t3.Vgid := "AAA";

  var t4: TestTypeWrapper := new TestTypeWrapper;
  t4.Vgid := "AAA";
end;

end.

But TestTypeWrapper implements ITestType and therefore I think it should have a Vgid property, even when it is internally accessible via TestType.

Doesn’t it work if you cast t3 to an ITestType ?

ITestType (t3).Vgid := 'AAA';

Oh, it does, but that does not solve my problem :wink:

For me, it seems clear:
TestTypeWrapper implements ITestType, so if something else sees it as a ITestType, it can see the Vgid property . As it soesn’t implement directly Vgid, something that sees it directly (without using the interface), sees the TestType property and not the Vgid one.

Normally it’s hidden,but if you want this behavior:

property TestType: ITestType read fTestType; implements public ITestType;

Patrick, but TestTypeWrapper implements ITestType: TestTypeWrapper = class(ITestType), so it implements ITestType…

Carlo, I applied your suggestion: same result. I also tried with additionally making property TestType public.

Thanks, logged as bugs://77807

bugs://77807 got closed with status fixed.

Fixed for v10