Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • Leaders
Posted

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)

:)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...