On 11/11/2016 10:14 PM, Ryan Joseph wrote:
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;
You have two options if you have a list of TObject.
1. (Arr.GetObject(n) as IMyInterface).DoSomething;
2. TMyObjectClass(Arr.GetObject(n)).DoSomething;
2 is kind of pointless because the interface is not being used so why
have it.
Probably it is better to keep a list of IMyInterface rather than TObject.
I made a small test and uploaded it to github if you want to check it
out. It uses a list of IMyInterface.
https://github.com/andrewd207/interfacetest
Or as a zip:
https://github.com/andrewd207/interfacetest/archive/master.zip
Regards,
Andrew Haines
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal