Francois Piette recently posted a solution to obtaining the screen position of a menu item involving using a “hacker” class. There is however a safer, more direct mechanism which I hope Francois won’t mind me sharing and a far less safe related hacking technique that his post brought to mind.
Mat DeLong just posted another great example of when not to abuse class helpers in Delphi (though I should add that he didn’t seem to see it that way). 🙂 But you don’t need helpers to do what this technique achieves, and in my view you really shouldn’t be using helpers for it in the first place.
NEWSFLASH: I believe I have found a use for class helpers in the wild! This, even though I have long held the view that class helpers should not be viewed as a general purpose utility. They were designed for a very specific purpose and the authors of the technology themselves tell us how they are intended to be used. Using them in other ways is asking for trouble: they can break your – or others – code, they hide details of an implementation in A Bad Way™ and you are likely to run into limitations because what you want to use them for doesn’t tally with what they are intended to be used for. But, somewhat to my surprise, I have recently found what I consider to be a legitimate use for them. Less surprisingly, it actually fits with their intended use.
My previous post on class helpers provoked a passionate response from some quarters who believed that they could be used “safely”. More worrying was an apparent belief that their use was actually endorsed by CodeGear – tacitly or otherwise. A rather odd view given the advice from CodeGear themselves is to not use them. No-one actually described a safe usage scenario though and those scenarios that were described all contained immediately apparent flaws of their own. However, I have realised that there is, if not a safe way then at least a responsible one, to create class helpers. Using them is still not entirely safe however.
Class helpers (introduced in Delphi 2007 2006 2005 – thanks to Chris and Bruce for the corrections) seem to be cropping up more and more frequently in suggested work-arounds or implementation approaches. I find this worrying given that this language feature has always come with the admonition from CodeGear that it isn’t advisable to use it! So why do people seem so keen on using them, and why shouldn’t they?