Wednesday, June 22, 2005

delegate :)

That's what everyone would like to do, right?
i will write mostly techie stuff here...see the name??? :-)

Here is a bit from the C# world. delegate is a keyword and more...

What does a dictionary say a delegate is?
We probably just want to see what the verb form means: to give power to someone to do something. Moving a little ahead: someone gives power to someone else! So, there are 2 sides to the delegate!

Bingo! we just hit the basic thing for a delegate now!!! 2 sides...so, in OO terms, we need 2 objects.

Just as an aside, i once had this situation wherein i wanted a static delegate and i even tried put to in such neat looking code that would never compile :) ...i realized the mistake!!! at times, i go off in such a patch applying mode that i just want to get things done :) i believe it is good to spot mistakes you made and correct them! Back here then.

delegates are perfectly OO is what MS says, and so they are once you realize this much. so, when you subscribe to a delegate, what you are doing is sending your identity to the delegate provider, who in turn can use your identity to delegate things when needed (it is like being pro-active to get more work from someone who is looking out for people to get their job done :-) ).

During program exection, your object's instance is stored with the provider and that instance is used to invoke a method inside your object (Now you ask: How else can you invoke an object's method? Now...that triggers a Q: can my method be a specified as a private accessor?)

Aren't delegates simple enough now? Delegation is always simple...once you know who is going to do your job ;-)

Adding some more because my friend Karthik wanted a little more clarity! We both agree that pasting a piece of code will not suite, so here is an example:
Say, there is a date provider who has a delegate that my application (in this case, a license validator) subscribes to. Suddenly, there is a date change, so, the provider fires off the delegate and it lands up right in my app. This is when (and where) I can do the kind of validations for this date change. In essence, date provider delegated this change to me!

-ramanarpanamastu

0 Comments:

Post a Comment

<< Home