Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's probably no help, this question was asked ages ago, but for anyone checking this out in future...</p> <p>You've probably just posted test code but your interface should contain a GUID, the GUID uniquely defines the interface to the compiler (Ctrl+Shift+G in Delphi 2009).</p> <pre><code>IDummy&lt;T&gt; = interface ['{F9EF740B-FF23-465A-A2E0-E2ACD5ABD90F}'] procedure DoSomething; end; </code></pre> <p>Hard casting is generally unsafe. A hard cast is only really acceptable where you know that an objects type will be correct. It's preferable when casting to check its type before the cast as follows...</p> <pre><code>var lTypeA: TTypeA; begin if ObjectA is TTypeA then begin lTypeA := TTypeA(ObjectA); end; </code></pre> <p>Even better I'd perform an "as" cast, which I think will cause an exception if it is invalid. This REALLY is preferable! I've written code that performs a hardcast only to spend hours and hours figuring out that actually my cast was wrong... Delphi won't tell you if you cast to the wrong type, then when you later use the object you end up in a whole debugging nightmare. The as will raise an exception and guide you to the problem in your code</p> <pre><code>var lTypeA: TTypeA; begin if ObjectA is TTypeA then begin // Will raise an exception if ObjectA is not TTypeA, // in this simple case the ObjectA is TTypeA test is redundant lTypeA := ObjectA as TTypeA; end; </code></pre> <p>I'd really only cast between objects in delphi. Delphi has a helpful function "supports" which will determine whether an object implements an interface and give you back an instance of that interface. You can then use the local variable returned to perform whatever function you need from the interface</p> <pre><code>if Supports(ImplBroken, IThisBreaksIt, lObjInterface) then begin lObjInterface.DoSomething; end; </code></pre> <p>Code in full...</p> <pre><code>type // Generic IDummy interface IDummy&lt;T&gt; = interface ['{F9EF740B-FF23-465A-A2E0-E2ACD5ABD90F}'] procedure DoSomething; end; // Don't alias interfaces if possible // This is a more specific version of your interface IThisBreaksIt = interface(IDummy&lt;Byte&gt;) ['{76EFA371-4674-4190-8A4B-06850103C1D8}'] end; TMyRecord = record // I would suggest, if you can avoid it don't store interfaces // in a record, just simple types - just my opinion, delphi doesn't stop you Intf: IDummy&lt;Byte&gt;; end; // Remember this is interfaced, so the object only exists while refcount &gt; 0 TDummy = class(TInterfacedObject, IDummy&lt;Byte&gt;) protected { IDummy&lt;T&gt; } // interface methods should be protected // So ideally we refer to the interface of this object, // not publicly available methods procedure DoSomething; public end; var ARecord: TMyRecord; Item: IDummy&lt;Byte&gt;; // Think this should be IThisBreaksIt ImplWorks:TDummy; ImplBroken: IThisBreaksIt; lObjInterface: IThisBreaksIt; begin ARecord.Intf := TDummy.Create; Item := ARecord.Intf; // Nasty hard cast here - what if your interfaced object is destroyed // before you reach this code section? ImplWorks := TDummy(Item); // This line compiles, buts it's really not the right way to get the interface ImplBroken := IThisBreaksIt(ARecord.Intf); // I believe this is the right way to get the interface for an object if Supports(ARecord.Intf, IThisBreaksIt, lObjInterface) then begin lObjInterface.DoSomething; end; end; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload