🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.pascal.delphi.misc
Article #718

Interfaces and object storage.

#718
From: Pyt
Date: Thu, 01 Jun 2000 00:00
85 lines
3283 bytes
In Delphi 4.0, I'am trying to store in a stringlist an object responding
to an interface IReactToTimer. But the compiler give me an error
something like "incompatible type IReactoTimer, tobject"

I read that it was not possible to cast a interface to an object. I do
not need to cast my interface but just to store only objects responding
to specify interface. To get the maximum chances to have a clear code at
compile time I don't want to accept directly tObject instead of
IReactoTimer. The most error delphi found a compile time the less error
I will got a run-time. Problably the stringlist is not the way to use to
store my object, is there an other way to have same functionnality ? 

Initially in delphi 2.0 tTimerObject was heriting from a class
tReactToTimer and it was working fine. I've read that interfaces were
build exactly in so cases. 

The code:

Type
  IReactToTimer = interface(IUnknown)
    procedure TimerNotification;
  end;

Type
    tTimerObject = Class(TObject)
  private
    { Private declarations }
    RegisteredObj:tStringList;             
  public
    { public declarations }
    constructor Create;
    Destructor Destroy; override;
    Procedure RegisterToTimer(AObject:
IReactToTimer;TypeNotification:Integer);
    Procedure UnRegisterTimer(AObject: IReactToTimer);
    Procedure Event;
  end;

implementation

{ -------------------------------------------------------------------
                                                                Timer
  ------------------------------------------------------------------- }
constructor tTimerObject.Create;
begin
  Inherited Create;
  RegisteredObj:=tStringList.Create;
end;

Destructor tTimerObject.Destroy;
begin
  RegisteredObj.Free;
  inherited Destroy;
end;

Procedure tTimerObject.RegisterToTimer(AObject:
IReactToTimer;TypeNotification:Integer);
begin
  RegisteredObj.AddObject(IntToStr(TypeNotification),AObject);
end;

Procedure tTimerObject.UnRegisterTimer(AObject: IReactToTimer);
Var lIndex:Integer;
begin
  lIndex := RegisteredObj.IndexOfObject(AObject);
  If (lIndex <> -1) then RegisteredObj.Delete(lIndex);
end;

Procedure tTimerObject.Event;
Var i:Integer;
begin
  With RegisteredObj do
  begin
    For i:=0 to RegisteredObj.Count-1 do
    begin
      (Objects[I] as IReactToTimer).TimerNotification;
    end;
  end;
end;

(Send me directly a copy of the answer, i've got some problem with the
news reader) 

Kind regards
Eric.
Technical Details
Message-ID: <393681FD.A3FCAA5E@Wanadoo.fr>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10a726,224f0e92a4b1141e,start
X-Google-Attributes: gid10a726,public
X-Google-Thread: 10baee,224f0e92a4b1141e,start
X-Google-Attributes: gid10baee,public
From: Pyt <pyt.stbg@Wanadoo.fr>
Subject: Interfaces and object storage.
Date: 2000/06/01
Message-ID: <393681FD.A3FCAA5E@Wanadoo.fr>
X-Deja-AN: 629914894
Content-Transfer-Encoding: 7bit
X-Accept-Language: fr,en
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: abuse@wanadoo.fr
X-Trace: wanadoo.fr 959873738 8038 193.250.245.77 (1 Jun 2000 15:35:38 GMT)
Organization: Wanadoo, l'internet avec France Telecom
Mime-Version: 1.0
NNTP-Posting-Date: 1 Jun 2000 15:35:38 GMT
Newsgroups: comp.lang.pascal.delphi.misc,fr.comp.lang.pascal