mookie Posted February 18, 2005 Posted February 18, 2005 Im looking at some code through reflector and its using a delegate.... I dont know what its used for and how its created as i dont have the option for just creating a delegate file allthough thats how it looks to me. Quote
Leaders Iceplug Posted February 18, 2005 Leaders Posted February 18, 2005 A delegate is what you use to declare how an event procedure should be declared and then, using these declarations, it connects an event that was raised to an event procedure. Example that I am using: Public Delegate Sub HPChangedEventHandler(ByVal sender As Object, ByVal e As HPDiffEventArgs) In my class, we declare the delegate's event that it is going to handle. Public Event HPChanged As HPChangedEventHandler And then, the event is raised. RaiseEvent HPChanged(Me, New HPDiffEventArgs(DMG)) HPDiffEventArgs is a class that I made. This event happens to be handled in another class that I have: AddHandler Value.HPChanged, New HPChangedEventHandler(AddressOf At_HPChange) Value is an instance of the previous class. And the At_HPChange subroutine: Private Sub At_HPChange(ByVal sender As Object, ByVal e As HPDiffEventArgs) End Sub So, we have declarations: Public Delegate Sub HPChangedEventHandler(ByVal sender As Object, ByVal e As HPDiffEventArgs) Outside of my classes Public Event HPChanged As HPChangedEventHandler In the class that raises this event. So, when the event is raised here: RaiseEvent HPChanged(Me, New HPDiffEventArgs(DMG)) The HPChanged EventHandler aka HPChanged delegate will pick up the info here ( Me, New HPDiffEventArgs(DMG) ) and then deliver it to the subroutine that handles this event so this sub is called Private Sub At_HPChange(ByVal sender As Object, ByVal e As HPDiffEventArgs) End Sub because the delegate is requested to go to this subroutine when the event is raised because of the AddHandler AddHandler Value.HPChanged, New HPChangedEventHandler(AddressOf At_HPChange) :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
IngisKahn Posted February 19, 2005 Posted February 19, 2005 More that that, you can think of delegates as type safe function pointers. Get .NET 2.0, anonymous delegates are great! Quote "Who is John Galt?"
*Experts* DiverDan Posted February 19, 2005 *Experts* Posted February 19, 2005 Great explaination and examples Iceplug!!!! Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.