[fpc-pascal] Re: Generics - how to rewrite TOjectDictionary
skalogryz.lists at gmail.com
Mon Apr 8 11:52:28 CEST 2013
You can always use functions, to fight typecasting.
function GetMyObject(dic: TStrings; const nm: string): TMyObject;
i : integer;
if not Assigned(dic) then begin Result:=nil; Exit; end;
i:=dic.indexof(nm); // replace with IndexOfName if necessary
if (i<0) or (i>=dic.Count) or not (i.Objects[i] is TMyObject) then
else Result:=TMyObject(i.Objects[i]); // not using "as" , since "is" has
already been used
Of course, people would use ClassHelpers these days to have nice syntax and
avoid the sanity check.
On Mon, Apr 8, 2013 at 1:42 AM, Marius2 <fpclazarus at home.nl> wrote:
> Thanks Dimitri,
> Yes, I have used it lots of times and its really the last choice if theres
> something available like the objectdictionary. In this case the dict is
> the owner of the objects, it saves me the continious ugly typecasting,
> care of the extra administration with freeing objects and all the
> troubles you get over time with this solution (its not as bad as i'm
> now). In other words tstrings.objects are working fine, but i find it a bad
> strategy if you have to use it in flatout in the whole application.
> I'm curious how to rewrite/solve dictionaries, indexed list and even sorted
> strings.objects to a neat solution while (if possible) working with both
> delphi and fpc/laz.
> View this message in context:
> Sent from the Free Pascal - General mailing list archive at Nabble.com.
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the fpc-pascal