tag:blogger.com,1999:blog-29331675.post9016749953005659742..comments2024-03-05T17:37:00.995+01:00Comments on The Delphi Geek: Walking the key-value containergabr42http://www.blogger.com/profile/06903558857617342477noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-29331675.post-20643034847595977452008-03-27T13:53:00.000+01:002008-03-27T13:53:00.000+01:00IMO, if you own the class you want to help - deriv...IMO, if you own the class you want to help - derive and override instead of "helping".<BR/><BR/>A stumbling block for class helpers is that you can only help the class once in the same scope. In other words - you can kiss polymorphism goodbye for that class in that scope.<BR/><BR/>IMO, class helpers - although nice - should be considered a last resort.Lars Fosdalhttps://www.blogger.com/profile/05635001623287214775noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-76425193247000053112008-03-22T23:32:00.000+01:002008-03-22T23:32:00.000+01:00No, that's definitely NOT what I had in mind by in...No, that's definitely NOT what I had in mind by introducing the class helper.<BR/><BR/>There is unit GpLists with my lists, enumerators and with the TGpKeyValue class.<BR/><BR/>Then you use the TGpIntegerObjectList class in some other unit, let's call it unit A. You store some objects inside Objects[] property. In my example I stored TGpString there.<BR/><BR/>In your code in unit A that uses TGpString stored in Objects[], you can use<BR/><BR/>for kv in list do<BR/> DoSomething(TGpString(kv.Value).Value);<BR/><BR/>To simplify this, you can introduce the class helper - and you implement it in the same unit A! So it is defined (it even doesn't have to be declared in the interface section) and used in the same unit. It only serves as a kind of strong-typed macro, nothing more.<BR/><BR/>There is no sense in modifying GpLists unit to achieve the same effect, I think you'll admit to that.gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-1154107885700850262008-03-22T22:54:00.000+01:002008-03-22T22:54:00.000+01:00Oh dear, class helpers again.Why is it so importan...Oh dear, class helpers again.<BR/><BR/>Why is it so important to not have to modify the class you are "helping", if you "own the class yourself.<BR/><BR/>All you have done it make it harder to use that class (since you presumably place the helper in a separate unit, otherwise the avoidance of modifying the source itself is a bit pointless).<BR/><BR/>So now code that uses that class has to use two units, instead of just using the unit that contains the class.<BR/><BR/>And then when someone else want to add some more capabilities to that class without modifying the source, now you have 3 units to use in order to use one class.<BR/><BR/>And nothing in the actual source of the class is going to tell you which other units you need in order to "complete" the class.<BR/><BR/><BR/>Which is why class helpers (and partial classes, which suffer very similar issues) are a great hack for modifying classes to which you don't have source access, but are a flat out CRAZY idea if you have access to that source.<BR/><BR/>You just create usage and maintenance headaches for your future.<BR/><BR/>And as I have commented on before, code spends the majority of it's life being used and maintained, and only a vanishingly small fraction of it's life being created.<BR/><BR/>Other than that, a GREAT example this time.<BR/><BR/>:)Anonymousnoreply@blogger.com