Re: Interface Question



Maarten Wiltink wrote:

[...] If you want to cast an interface reference into an object
reference, simply implement an according interface method, which
does nothing but Result := Self;

It's not considered good style, however. Once you're doing things
through interfaces, you shouldn't go back to object references lest
you save one somewhere and it collapses under your feet because the
original interface reference went out of scope somewhere else and
the reference counting kicked in.

That's right, but there exist special cases.

One may want to use the objects in the first place, but needs interfaces for some "external" requirement. Then refcounting can be disabled in the object, what makes them live until they are explicitly destroyed, as usual. Otherwise it certainly is not a good idea, to store object references for reference counted objects.

Often it's desireable to get an idea, what object hides behind some small interface, as it might be interesting to know what object hides behind a Sender:TObject in an event handler. Once you get the object, you can retrieve its class type or name, and proceed accordingly. This procedure is safe, as long as the object is retrieved whenever needed, and no object reference is stored for later reuse.

DoDi
.



Relevant Pages

  • Re: 7.0 wishlist?
    ... The "auto-implement" is intended mainly for the odd situation where an existing class you can't edit fits some interface and you're willing to take responsibility for it if it turns out not to actually adhere to the contract, and try using it where that interface type is expected. ... If reference declarations started showing up with the odd asterisk, bang, or other punctuation mark on it, but never primitive declarations, people would probably be able to guess what was going on, on the basis of "what other binary flag might be set on references but not primitives and would be really useful besides can be/cannot be null?" ... the compiler cannot prove by static analysis that the RHS isn't null might be a good idea. ... Object foo, bar; ...
    (comp.lang.java.programmer)
  • Re: Anders Hejlsberg comment on immutable objects
    ... >explicit interface implementation exists is so an interface name can class ... Improper implicit conversions constitute a nasty ... >> With value types, the variable's value is the object, not a reference ... Consider the affects of our different views on a const ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: please try this program (generic resolution)
    ... of the specific interfaces of a generic interface that has that name and ... either is in the scoping unit in which the reference appears or is made ... And now in my example I misspelled ENISOC. ... external fun ...
    (comp.lang.fortran)
  • Re: C# Plugin system - same interface in two different assemblies...
    ... "add a reference to the appropriate assemblies to each and every project"? ... You don't have each plugin project deciding what ... its idea of the interface is. ... can't by forcing people to include them in assemblies as I've explained. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using early-bound interface on a late-bound object
    ... > the compiler determines which interfaces to use, ... > supports the declared interface. ... >> help if someone can point me to some authoritative document or reference ... within customer shops when they mirrored this 'division' to keep their ...
    (microsoft.public.vb.general.discussion)