Okay, here is an example. To a module, add a delegate Sub
TimerActionHandler(t as Timer). Next, define a subclass Delegating
Timer as Timer. Add a public property ActionHandler as
TimerActionHandler. Then implement its Action event handler as follows:
Sub Action()
if me.ActionHandler <> nil then
me.ActionHandler.Invoke me
end if
End Sub
You now have a timer that allows you to plug in behavior at runtime.
My column in the upcoming issue of REALbasic Developer offers a few
brief examples of how one might use delegates. Also, my update to
Sparkle for REALbasic uses delegates to handle some events; you might
take a look at that.
Charles Yeomans
On Dec 30, 2007, at 9:02 PM, Wade Maxfield wrote:
> OOPS! Sorry.
>
> The wording was stupid. <grin> It was wrong as I said it.
>
> It is not intuitively easy to change what code a method calls in
> C++ at run time either, but it can be done by using pointers.
>
> in C/C++, you can have pointers to functions. You can then put
> these pointers to functions in classes as properties, and then have
> class methods call through the function pointers. Change the pointer
> while running and you get a new function at run time. *Warning* Very,
> very easy to crash your system that way! There are linker and
> inheritance issues when doing this, so it is not always a free ride.
>
> I think that RB can do close to this using delegates, I just don't
> have my mind wrapped around delegates yet. I'm a very example
> oriented person, and I don't have enough examples yet (and havent'
> studied it yet either.)
>
> However, I think I am on the right track. Many thanks to everyone!
>
> thanks!
> Wade
>
>> I've not been able to get through my head an issue regarding
>> overriding methods at runtime for classes I instantiated at runtime.
>> With C++ it is easy. With RB, I don't know how to do it. Call me
>> stupid, I don't care.
>
> Date: Sat, 29 Dec 2007 18:41:09 +0900
> From: Andy Dent <dent at oofile dot com dot au>
> <snip>
>
> Wade, I *think* I understand what you're saying but, frankly, your
> first sentence doesn't work for C++ either. Please forgive me if this
> sounds a bit condescending but I just wanted to correct the impression
> you gave for anyone reading who doesn't know C++.
> <snip>
> Andy
> _______________________________________________
> Unsubscribe or switch delivery mode:
> <http://www.realsoftware.com/support/listmanager/>
>
> Search the archives:
> <http://support.realsoftware.com/listarchives/lists.html>
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
|