[fpc-devel] Which of them are right (Delphi or FPC)

Vincent Snijders vsnijders at quicknet.nl
Tue Apr 18 09:02:41 CEST 2006


Amir Aavani wrote:
> I developed a project in lazarus (FPC) and now when I try to run it in 
> Delphi, it acts incorrect!
> 
> I have a class TObjectCollection which is a collection of TObject:
> TObjectCollection= class (TObject)
>      FMembers: array of TObject;
>       ...
>        ...
>    public
>      property Member [Index: Integer]: TObject read GetMember;
>      ...
>      procedure Add (ANewMember: TObject);
>      ...
>  end;
> 
> And A TObject1Collection which is like this:
> TObject1Collection= class (TObjectCollection)
>      function GetMember: TObject1;
>       ...
>        ...
>    public
>      property Member [Index: Integer]: TObject1 read GetMember;
>      ...
>      procedure Add (ANewMember: TObject1);
>      ...
>  end;
> 
> procedure TObject1Collection.Add (ANewMember: TObject1);
> begin
>  (Self AS TObjectCollection).Add (ANewMember AS TObject);
> end;
> 
> procedure TObject1Collection.GetMember: TObject1;
> begin
>  Result:= (Self AS TObjectCollection).Member AS TObject1;end;
> end;
> 
> The code works correctly in lazarus, but in Delphi, the procedure 
> TObject1Collection.Add call itself instead of TObjectCollection.Add. I 
> changed the code like this:
>  SelfAsTObjectCollection:= Self As TObjectCollection;
>  SelfAsTObjectCollection.Add (ANewMember);
> and the problem solved.
> I want to know, which of us (me, Delphi, Freepascal) is wrong.

I don't know.

Instead of
   (Self AS TObjectCollection).Add (ANewMember AS TObject);
you could use
   Inherited Add(ANewMember as TObject).

And because TObject1 inherits from TObject (at least in this example), 
it can be even shorter:
   Inherited Add(TObject).

I don't know if you can use inherited with propeties. I would have to 
check to docs for that.

Vincent



More information about the fpc-devel mailing list