?
Solved

Putting interfaces in a List

Posted on 2003-02-20
8
Medium Priority
?
643 Views
Last Modified: 2010-04-04
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
Comment
Question by:pfeilgm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Author Comment

by:pfeilgm
ID: 7989680
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
 
LVL 4

Expert Comment

by:StevenB
ID: 7990122
 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
 

Author Comment

by:pfeilgm
ID: 7990238
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
Independent Software Vendors: 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!

 

Author Comment

by:pfeilgm
ID: 7990254
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
 
LVL 3

Expert Comment

by:sfock
ID: 7992696
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
 

Expert Comment

by:CleanupPing
ID: 9317152
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
 
LVL 10

Expert Comment

by:kacor
ID: 9928923
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
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 9974932
PAQed, with points refunded (50)

Computer101
E-E Admin
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question