Link to home
Start Free TrialLog in
Avatar of pfeilgm
pfeilgm

asked on

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.
Avatar of pfeilgm
pfeilgm

ASKER

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.
Avatar of StevenB
 You may also find some of the resposes given to my identical question here are of interest :o)

https://www.experts-exchange.com/questions/20404846/Adding-Interfaces-to-Lists.html

  Steven
Avatar of pfeilgm

ASKER

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.
Avatar of pfeilgm

ASKER

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.
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 ;-)
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.
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
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial