Hack delphi
A mock-up example would be:. We want to call the protected Useful method, but all we got access to is the public Boring method. Typical, right? A final twist is to use the same class name, like this:. Now you only need to add the MyUtility reference in the uses clause after the Utility reference. However, sometimes you cannot control the type of the instance used - it is created inside a body of 3rd party code, for instance. As many experienced Delphi programmers will know, you can easily gain access to the protected members of a class instance, simply by casting the instance to a descendent class type declared in the same unit as the cast-expression.
For instance, to call the protected Useful method of an instance of the TUtility class you can just:. The way the Delphi visibility directives work, all code in the same unit as the declared class will gain full public access to all members.
Thanks to the ingeniousness of Borland's compiler developers this common hack even works in. NET with Delphi 8 - as long as the caller and method is in the same assembly - it does not work across assemblies, because protected maps to the family-or-assembly method attribute in the CLR. Then in all their own units that need access to the methods, they turn around and perform this local-descendent-cast hack and their own classes. This hack should only be used to get access to other people's code, and not used between your own units!
Interestingly, the compiler only uses the local hack-class to get access to the protected method. Otherwise this hack would be very expensive with regards to code-size.
Labels: C , Hack , Win Is there a reason properties aren't used to expose protected methods and fields instead of this class hack?
Not sure what you mean here, Chris? How do you expose "procedure Useful;" as a property? Add a comment. Active Oldest Votes. FieldByName 'Name' ; To determine which cell is under the mouse cursor at the moment, you can use MouseCoord , which returns a TGridCoord record holding the coordinates of the cell under the cursor. X; Setting the row is trickier, and so far the only way I could find involves the so called protected hack , the method of accessing protected properties and methods of a class.
Here's how: MyDataSet. MoveBy Cell. Improve this answer. Andriy M Andriy M 73k 16 16 gold badges 90 90 silver badges bronze badges. I need to change the currently active cell first - that's where I am stuck. Specifically I need the cell under the mouse cursor. I edited the question to be more specific about this. HeinrichUlbricht: Updated with a way of finding the cell under the mouse cursor. Getting closer : Y is still being ignored. I need to be able to freely specify the selected cell - this includes row and column or X and Y.
HeinrichUlbricht: Yeah, saw that coming. Setting the row would be much trickier. Right, back to the drawing board then. DataSource or not Assigned Grid. Row; Grid. Options then Grid.
X, CellAtMousePos. Y ; end; The grid selection follows the mouse cursor. Works like a charm from a technical standpoint. Usability is another question. Community Bot 1 1 1 silver badge. Sign up or log in Sign up using Google. All keys are sound activated.. Virus scans: virustotal. Last edited by Departure; at AM. Fix the virus scan links. Originally Posted by Disturbed. Originally Posted by UnknownCoder. But what is this? The source?
0コメント