You hardly can find a sample of delegate in books. Also in my classes people ask me “Could you give us some real example of delegate?” Ok this is a real sample of delegate that I used for one of my applications:
First let’s understand the application:
The application was a web application. It had a master page with 6 tabs and 3 buttons (Save all, submit data, Exit). Each tab had to show different information so I had 6 pages using this master page. Client asked me to save information when another tab is selected or when they click on save all or submit data (when submit data was selected just application was redirect to another part).Any way my situation was like this some buttons in master page and also in pages I had a save method to save all information about that page. I needed to call save method of the pages when in master page one of those buttons was clicked. Like this:
So I created a saved delegate. In master page I had an instance of this delegate then in button clicks I called that delegate. In each page load I registered the save method into delegate so when in master page saved delegate is called then this save method of page is called actually. Master page does not know what is the method registered in delegate. It is just calling delegate. In fact pages register their own save method in delegate so when delegate is called save method will be called.
No comments:
Post a Comment