I've created several simple lists (and integer list and color list) yet when I try to make an "extended" list it says invalid typecast even though I've used similar typecasting for the prior 2 lists (it throws the error wherever I use the Extended() typecast).
Type
TAExtList = Class(TObject)
Private
FList: TList;
Procedure SetExt(Index: Integer; Value: Extended);
Function GetCnt: Integer;
Function GetExt(Index: Integer): Extended;
Public
Constructor Create;
Destructor Destroy; Override;
Function Add(Value: Extended): Integer;
Function Insert(Index: Integer; Value: Extended): Integer;
Procedure Delete(Index: Integer);
Procedure Clear;
Function IndexOf(Value: Extended): Integer;
Property Count: Integer Read GetCnt;
Property Extendeds[Index: Integer]: Extended Read GetExt Write SetExt; Default;
End;
Function TAExtList.Add(Value: Extended): Integer;
Begin
Result := FList.Add(Pointer(Value));
End;
There are several reasons. First, as MBo already wrote in his answer, an
Extendedis 10 bytes in size, so it doesn't fit in the 32 bits (4 bytes) of aPointer.Another reason is that in Delphi, direct hard casts to floating point types are not allowed, because too many C programmers expected the cast to do a conversion, instead of just a reinterpretation of the bytes of the Extended (or other floating point type). Although the sizes would match, casting to
Singleis not allowed either. Very early versions of Delphi did actually allow these casts, if I remember correctly (or was that Turbo Pascal?).But you can still create your
TExtendedList, if you use pointers toExtended(after all, aTListstores pointers):But this means that your
TListnow contains pointers toExtendeds that are allocated on the heap. So removing or changing requires you to useat the appropriate places (e.g. in
Delete(),Remove(),Extract(),Clear, etc. and the destructor). Each of the allocatedExtendeds must be disposed of at the right time.Retrieving is similar:
Etc., etc. ...
Update
As @kobik said, you could use the virtual Notify function to delete the items that are removed, instead of in each method: