[fpc-pascal] Re: Generics - how to rewrite TOjectDictionary
dmitry boyarintsev
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;
var
i : integer;
begin
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
Result:=nil
else Result:=TMyObject(i.Objects[i]); // not using "as" , since "is" has
already been used
end;
Of course, people would use ClassHelpers these days to have nice syntax and
avoid the sanity check.
thanks,
Dmitry
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
> also
> the owner of the objects, it saves me the continious ugly typecasting,
> takes
> care of the extra administration with freeing objects and all the
> additional
> troubles you get over time with this solution (its not as bad as i'm
> telling
> 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.
>
> Greetings,
> Marius
>
>
>
>
>
> --
> View this message in context:
> http://free-pascal-general.1045716.n5.nabble.com/Generics-how-to-rewrite-TOjectDictionary-tp5714029p5714031.html
> Sent from the Free Pascal - General mailing list archive at Nabble.com.
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130408/ada4b851/attachment.html>
More information about the fpc-pascal
mailing list