<div dir="ltr">please disregard my last note about "avoiding the sanity check"<div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Apr 8, 2013 at 1:52 PM, dmitry boyarintsev <span dir="ltr"><<a href="mailto:skalogryz.lists@gmail.com" target="_blank">skalogryz.lists@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">You can always use functions, to fight typecasting.<div><br></div><div>function GetMyObject(dic: TStrings; const nm: string): TMyObject;</div>
<div>var </div><div>  i : integer;</div><div>
begin</div><div>  if not Assigned(dic) then begin Result:=nil; Exit; end;</div><div>  i:=dic.indexof(nm); // replace with IndexOfName if necessary</div><div>  if (i<0) or (i>=dic.Count) or not (i.Objects[i] is TMyObject) then Result:=nil</div>

<div>  else Result:=TMyObject(i.Objects[i]); // not using "as" , since "is" has already been used</div><div>end;</div><div><br></div><div>Of course, people would use ClassHelpers these days to have nice syntax and avoid the sanity check.</div>

<div><br></div><div>thanks,</div><div>Dmitry</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Apr 8, 2013 at 1:42 AM, Marius2 <span dir="ltr"><<a href="mailto:fpclazarus@home.nl" target="_blank">fpclazarus@home.nl</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks Dimitri,<br>
<br>
Yes, I have used it lots of times and its really the last choice if theres<br>
something available like the objectdictionary. In this case the dict is also<br>
the owner of the objects, it saves me the continious ugly typecasting, takes<br>
care of the extra administration with freeing objects and all the additional<br>
troubles you get over time with this solution (its not as bad as i'm telling<br>
now). In other words tstrings.objects are working fine, but i find it a bad<br>
strategy if you have to use it in flatout in the whole application.<br>
<br>
I'm curious how to rewrite/solve dictionaries, indexed list and even sorted<br>
strings.objects to a neat solution while (if possible) working with both<br>
delphi and fpc/laz.<br>
<br>
Greetings,<br>
Marius<br>
<br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://free-pascal-general.1045716.n5.nabble.com/Generics-how-to-rewrite-TOjectDictionary-tp5714029p5714031.html" target="_blank">http://free-pascal-general.1045716.n5.nabble.com/Generics-how-to-rewrite-TOjectDictionary-tp5714029p5714031.html</a><br>


<div><div>Sent from the Free Pascal - General mailing list archive at Nabble.com.<br>
_______________________________________________<br>
fpc-pascal maillist  -  <a href="mailto:fpc-pascal@lists.freepascal.org" target="_blank">fpc-pascal@lists.freepascal.org</a><br>
<a href="http://lists.freepascal.org/mailman/listinfo/fpc-pascal" target="_blank">http://lists.freepascal.org/mailman/listinfo/fpc-pascal</a><br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>