Hi folks !!
We came to second example of our "Delegate" concept. Please read the first part (previous post) if you haven't done yet.
Heading of our second example is "I want to observe you". You can see real power of the delegate in this example.
Before digging into the source code, i will explain back ground of this example.
"A husband is there who suspects his wife that she got an affair with some one. He feels that her boyfriend resides in an apartment called "ThisIsLife". Daily following his wife when she goes out became a problem for the husband. Rather than he following her, he wanted to get information when ever she enters that Apartment.
For that, he executed one simple plan. He knows that when ever any visitor enters that apartment he need to write his/her details in the register. Our husband given bribe to the watchman of the apartment and asked him send message when ever any entry made into the register."
This is the scenario. This scenario has been translated into source code as shown below.

Here, there are two classes. Line 3- 12 is the Apartment class. As our doubtful husband given bribe to the watchman, he created one method in this class called "WriteInApartmentRegister".
What this method will do ?
It raises an Event.

How this event is created ?
Simple. Just in two lines using our "Delegate".

Here, in delegate declaration, one parameter called "name" has been defined. Our husband wanted to know name of the person entering to the apartment.You can declare delegate in your own way with number of parameters you need based on your requirement.
Now comes our husband class (Line 14-30).
He knows, Apartment object is having an event called "NewEntry" and it takes name of the person as parameter. He also knows, by simply hacking that event, he can get the name of the person that event is holding.

See what he has done. In the constructor of his class, he has taken Apartment object's NewEntry event and given address of his own method which is "IsSheMyWife". That means, when ever NewEntry event is raised "IsSheMyWife" method will be called.

Now he can happily check value in the name variable and take necessary action.
Now classes are ready. How to call them ?
As usual.

That's it. When ever "WriteInApartmentRegister method called, IsSheMyWife method will also be called.
Simple. Isn't it ?
It will be nice yaar. Just write this in code, debug and see how it works.
I'm sure. You will like it.
Shall i say "bye" for now ?
Bye!!
We came to second example of our "Delegate" concept. Please read the first part (previous post) if you haven't done yet.
Heading of our second example is "I want to observe you". You can see real power of the delegate in this example.
Before digging into the source code, i will explain back ground of this example.
"A husband is there who suspects his wife that she got an affair with some one. He feels that her boyfriend resides in an apartment called "ThisIsLife". Daily following his wife when she goes out became a problem for the husband. Rather than he following her, he wanted to get information when ever she enters that Apartment.
For that, he executed one simple plan. He knows that when ever any visitor enters that apartment he need to write his/her details in the register. Our husband given bribe to the watchman of the apartment and asked him send message when ever any entry made into the register."
This is the scenario. This scenario has been translated into source code as shown below.
Here, there are two classes. Line 3- 12 is the Apartment class. As our doubtful husband given bribe to the watchman, he created one method in this class called "WriteInApartmentRegister".
What this method will do ?
It raises an Event.
How this event is created ?
Simple. Just in two lines using our "Delegate".
Here, in delegate declaration, one parameter called "name" has been defined. Our husband wanted to know name of the person entering to the apartment.You can declare delegate in your own way with number of parameters you need based on your requirement.
Now comes our husband class (Line 14-30).
He knows, Apartment object is having an event called "NewEntry" and it takes name of the person as parameter. He also knows, by simply hacking that event, he can get the name of the person that event is holding.
See what he has done. In the constructor of his class, he has taken Apartment object's NewEntry event and given address of his own method which is "IsSheMyWife". That means, when ever NewEntry event is raised "IsSheMyWife" method will be called.
Now he can happily check value in the name variable and take necessary action.
Now classes are ready. How to call them ?
As usual.
That's it. When ever "WriteInApartmentRegister method called, IsSheMyWife method will also be called.
Simple. Isn't it ?
It will be nice yaar. Just write this in code, debug and see how it works.
I'm sure. You will like it.
Shall i say "bye" for now ?
Bye!!

















