> On Nov 12, 2016, at 3:22 AM, Jonas Maebe <jonas.ma...@elis.ugent.be> wrote: > > You're passing a class, not an object. Passing a class that implements an > interface to a method that expects that interface should work fine. > Converting a class instance to an interface that it implements is trivial and > always works, it's doing the opposite that is hard and that requires specific > support in the interface.
Here’s a better example of where this breaks. Yes casting does work to an extent but breaks eventually down the chain when I lose the reference. Is there anyway to get around this? Maybe I could store the reference from Supports inside TMyObjectClass so I don’t need to call Supports again? type IMyInterface = interface procedure DoSomething; function GetObject: TObject; end; type TMyObjectClass = class(IMyInterface) procedure DoSomething; function GetObject: TObject; end; var Obj: TMyObjectClass ObjInt: IMyInterface; Obj := TMyObject.Create; ObjInt := Obj; // this works fine, casting to an interface // Unit A only knows about IMyInterface so we pass // the class cast as an interface (not sure what to call this) DomeSomethingInUnitA(ObjInt); // Unit A var Obj: TObject; Arr: TArray; // collection class the stores TObject Arr := TArray.Create; Obj := ObjInt.GetObject; Arr.AddObject(Obj); [do some stuff] // here the problems start because TArray always returns TObject // and I can’t cast TObject to IMyInterface even though the object // stored in the array does in fact implement IMyInterface ObjInt := IMyInterface(Arr.GetObject(0)); // error! but I need to get a reference to IMyInterface from the array // I have to use “as” or Supports here and it defeats the purpose ObjInt := Arr.GetObject(0) as IMyInterface; Regards, Ryan Joseph _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal