🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.pascal.delphi.misc
1 messages
1 total messages Started by Pyt Thu, 01 Jun 2000 00:00
Interfaces and object storage.
#718
Author: 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.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads