Hiding Delegate Declarations in Classes
· Aug 2, 10:07 AMI use delegates a lot, so I was pleased to see that REALbasic 2008 Release 3 added the ability to add structures, enumerations, and delegates to classes as well as modules. After some experimentation, I am even more pleased.
I have a DelegatingTimer subclass of Timer that I use when I need a Timer. This reduces the number of Timer subclasses in my projects to one.
It is implemented as follows. First is a delegate, Sub TimerActionDelegate(t as DelegatingTimer). Second is a public property Action as TimerActionDelegate. And third is the implementation of the Timer.Action event handler.
Sub Action()
if me.Action <> nil then
me.Action.Invoke me
end if
End Sub
Until recently, I had to declare the delegate type in an auxiliary module. As I said, I use delegates a lot, so these modules have been piliing up. With the release of REALbasic 2008 r3, I can ditch the module and move the delegation declaration into the DelegatingTimer class. This makes the ‘Timer’ in ‘TimerActionDelegate redundant, so I can remove it without loss of clarity.
But the best part is that I can make the declaration private. I can still assign to the Action property in code outside the class.
dim t as new DelegatingTimer
t.Action = AddressOf InTheMidnightHour
But I can prevent any other use of the delegate type outside my class. For example, I can change its name without breaking any code. Obviously, I cannot change its signature without breaking code, but in such a case I would only break code that uses DelegatingTimers, which should limit the potential effects of such a change.
Comment
Commenting is closed for this article.
When Does a Window Have an Attached Sheet? Cocoa Support in REALbasic
Glad to have stumbled upon your blog. Interesting and very useful stuff you got here.
— myspace design · Aug 8, 08:42 AM · #
Thankyou!, I knew this feature existed but did not know how to code it.
— Andrew Peck · Mar 15, 05:07 PM · #