Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 645
  • Last Modified:

Putting interfaces in a List

I'm an experienced programmer, but I'm working on my first Delphi project, and I've run into what seems to be a simple misunderstanding on my part.

Here is a snippet of the code. I am unable to insert Objects implementing the ITimeListener interface into a TObjectList. Retrieving the objects is ugly, and so I guess I am doing something wrong.

Do I just misunderstand what interfaces are in Delphi?

//===============================================
interface
    TExternalTimer = class(TInterfacedObject, ITimeListener)
    private
        mListeners: TObjectList;
    protected
        procedure OnCorrectedTimeEvent(Bib: String; Channel: Integer; Time: TRaceTime);
    public
        procedure RegisterListener(Listener: ITimeListener);
    end;

implementation
procedure TExternalTimer.OnCorrectedTimeEvent(Bib: String; Channel: Integer; Time: TRaceTime);
var
    I: Integer;
begin
    for I := 0 to (mListeners.Count - 1) do
        begin
        ((mListeners[I] as TInterfacedObject) as ITimeListener)
            .OnCorrectedTimeEvent(Bib, Channel, Time);
        end;
end;

procedure TExternalTimer.RegisterListener(Listener: ITimeListener);
begin
    // This complains that I TObject and ITimeListener are incompatible.
    mListeners.Add(Listener);
end;
//===============================================

From what I've seen, Delphi doesn't seem to treat interfaces as a "view" into some concrete object. They are a different beast altogether and that, I think, is what's making my head hurt.
0
pfeilgm
Asked:
pfeilgm
1 Solution
 
pfeilgmAuthor Commented:
Ok, sorry. I fought with this for a day before I decided to post the question. Then, immediately after posting, I thought, "Hrmm, maybe there's a TInterfaceList." Which there is. So, it's all better now.

I'm just not used to interfaces being so different from classes.
0
 
StevenBCommented:
 You may also find some of the resposes given to my identical question here are of interest :o)

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20404846.html

  Steven
0
 
pfeilgmAuthor Commented:
Ok, sorry. I fought with this for a day before I decided to post the question. Then, immediately after posting, I thought, "Hrmm, maybe there's a TInterfaceList." Which there is. So, it's all better now.

I'm just not used to interfaces being so different from classes.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pfeilgmAuthor Commented:
Damn, refreshed the page, and reposted my last comment.

In any case, Thanks Steven. I swear I searched for almost that exact phrase before I posted this myself. It'd be nice if EE allowed you to search within a category, so it's not so hard to get a decent set of results.
0
 
sfockCommented:
after you found a solution yourself and the tread Steven pointed to shows up the solutions too just an additional comment:
>I'm just not used to interfaces being so different from classes.
Interfaces are always very different from classes but in delphi there is one additiona difference compared to java : Delphi classes don't support reference counting (and garbage collection) but interfaces do. Do never forget this when you are working with interface references and class references in Delphi.
ATList can store pointers only. But with this fearture you are naturely able to store any data you can poit to (wich means any data) but any additional features like reference counting (wich is also supported for long strings) are not supported by TList itself. You'll have to implement it yourself by your own need or use an proper descant already existing like TInterfaceList or TStringList

hope that helps to undersand it better ;-)
0
 
CleanupPingCommented:
pfeilgm:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
kacorretiredCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       PAQ and refund points
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Some days and here is the Christmas Time. I wish good luck and good health for you all and for your loved ones

kacor
EE Cleanup Volunteer
0
 
Computer101Commented:
PAQed, with points refunded (50)

Computer101
E-E Admin
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now