The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

Delphi XE introduced SafeIntfAsClass to cast back interface to class (via: oop – Delphi – Proxy Design Pattern – interface problem – Stack Overflow)

Posted by jpluimers on 2011/03/23

Delphi XE introduced the SafeIntAsClass to cast back from an interface to a class.

This is one of the things I really started to miss after doing a lot of .NET work (where that worked starting with .NET 1).

Older versions of Delphi did not support safely casting from an interface back to an object.

–jeroen

via: oop – Delphi – Proxy Design Pattern – interface problem – Stack Overflow.

4 Responses to “Delphi XE introduced SafeIntfAsClass to cast back interface to class (via: oop – Delphi – Proxy Design Pattern – interface problem – Stack Overflow)”

  1. Stefan's avatar

    Stefan said

    If you need to cast an interface reference back to an object reference, you’re doing something wrong design wise imo.

    • jpluimers's avatar

      jpluimers said

      You don’t need this often, but sometimes you want to get on the object side of your platform in stead of the interface side.

      This holds for any platform, Delphi, .NET, etc.

      –jeroen

  2. Chris Rolliston's avatar

    CR said

    It was D2010 actually (Allen Bauer blogged about it at the time). Moreover, your highlighting something called ‘SafeIntAsClass’ might be a bit confusing out of context, since the syntax is to use either an apparently hard cast or the ‘as’ operator [i.e., MyObj := TMyObj(MyIntf) or MyObj := MyIntf as TMyIntf]. If the class type queried for is incorrect, the ‘hard’ cast returns nil (like what Delphi .NET used to do) where the ‘as’ operator raises an exception.

    I agree it’s a neat little addition though, even if the old problems of the IInterface=IUnknown equation aren’t magically got rid of…

Leave a reply to jpluimers Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.