Results 1 to 6 of 6

Thread: Event and delegates

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Event and delegates

    Jus wanted to make sure I understood events and delagates correctly, I created this very silly test program (asp.net)

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim MyBell As New Bell()
    3.         Dim myE As EventArgs
    4.         Dim mypic As New Piccolo()
    5.  
    6.         AddHandler MyBell.Ring, AddressOf mypic.Responding_OnRing
    7.         MyBell.OnRing(myE)
    8.  
    9.     End Sub
    10.  
    11. End Class
    12.  
    13. Public Class Bell
    14.     Public Event Ring As EventHandler
    15.  
    16.     Public Overridable Sub OnRing(ByVal e As EventArgs)
    17.         RaiseEvent Ring(Me, e)
    18.     End Sub
    19.  
    20.    
    21. End Class
    22.  
    23. Public Class Piccolo
    24.     Public Sub Responding_OnRing(ByVal sender As Object, ByVal e As EventArgs)
    25.  
    26.     End Sub
    27. End Class

    I have two classes, the bell and the piccolo (hehe)

    * I define a standard event delegate called "Ring" in the Bell
    class

    * I create a method that raises the event (OnRing)

    * Then I create Piccolo class which should respond to the event

    * I define an event handler method (Responding_OnRing)

    * And finally attach the event to the piccolo objects eventhandler


    Is this the preferred way to do it? What if I wanted to write "on the way" on the webpage?? Do I have to write a new delegate that is capable of returning a string, instead of the standard EventHandler delegate? I tried to define a new delegate that handles return values but it failed when I tried to create the event, am I thinking wrong about this? Shouldn't event handlers be capable of returning values? In this case I want to piccolo to re

    VB Code:
    1. Public Delegate Function MyEventHandler(ByVal sender As Object, ByVal e As EventArgs) As String

    Whats up with the "MyBell.OnRing(myE)"... why do I have to pass an EventArg? Which one should I pass? In the code I create a new! I do not fully understand this part... care to explain?



    kind regards
    Henrik - trying to learn all about events and delegates in asp.net
    Last edited by MrNorth; Feb 25th, 2004 at 03:26 PM.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Your example shows using a delegate and event but I'm not sure what it is the preferred way of doing? What is it you events or delegates to do? A delegate can be a function but generally events do not return a value so they would not return a string. I guess I'm not clear as to what the question is. Or what you are trying to learn.

    As for the EventArgs if you don't use it or have none then you can pass Nothing or EventArgs.Empty. The basic eventing model for .NET is that all events generally have just 2 parameters sender as object (the object that raised the event) and e as EventArgs (any additionaly information needed in for or in the event). To keep with the basic model the generic EventHandler has an e (EventArgs) parameter even when its not used. This helps conform to a standard structure and helps with combining events from different objects.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Thanks for the reply! Im basically trying to learn how to use events, and I think I have succeeded with this new approach:

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim MyBell As New Bell()
    3.         Dim mypic As New Piccolo()
    4.  
    5.         AddHandler MyBell.Ring, AddressOf mypic.Responding_OnRing
    6.         AddHandler mypic.Coming, AddressOf Me.Responding_OnComing
    7.         MyBell.OnRing(e)
    8.  
    9.  
    10.     End Sub
    11.  
    12.     Public Sub Responding_OnComing(ByVal sender As Object, ByVal e As EventArgs)
    13.         Response.Write("on the way")
    14.     End Sub
    15.  
    16. End Class
    17.  
    18. Public Class Bell
    19.       Public Event Ring As EventHandler
    20.  
    21.     Public Overridable Sub OnRing(ByVal e As EventArgs)
    22.         RaiseEvent Ring(Me, e)
    23.     End Sub
    24.  
    25.  
    26. End Class
    27.  
    28. Public Class Piccolo
    29.     Public Event Coming As EventHandler
    30.     Public Sub Responding_OnRing(ByVal sender As Object, ByVal e As EventArgs)
    31.         onComing(e)
    32.     End Sub
    33.     Protected Sub onComing(ByVal e As EventArgs)
    34.         RaiseEvent Coming(Me, e)
    35.     End Sub
    36. End Class


    Picture that the page class is the one who is trigging the event ring, then the piccolo object is responding with a new event (coming) this event is handled by the page class who is printing "on the way"...

    Does this example properly shows the usage of events and delegates? I understand now how to play with events, handlers etc... but still not what to do with delegates.

    Keep in mind this is for my personal training only, don't expect this crazy stuff to be used in any commercial program Im writing
    Just wanted to find out if events/delegates could improve my code and design somehow


    kind regards
    Henrik

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    AddHandler is typically used to hook into events from an object that is added dynamically and does the exact samething as the Handles keyword at the end of a method. So this:

    AddHandler mypic.Coming, AddressOf Me.Responding_OnComing

    And:

    Public Sub Responding_OnComing(ByVal sender As Object, ByVal e As EventArgs) Handles mypic.Coming
    Response.Write("on the way")
    End Sub

    are the samething, only mypic would have to be declared at the page level instead of in a method. Since you add the object at runtime in the load event you do right by using AddHanlder.

    With delegates, unless you use C#, then for typical stuff you wont need them. Otherwise just know that an Event IS a delegate. Situations don't come up everyday that need a delegate but the idea is the same as a function pointer. In other words a variable that holds a method instead of data. Delegates are also an easy way to do threading since you can fire the delegate method on a seperate thread via the BeginInvoke method.

    You could pass a delegate to another form and have it call the method it references at a later time or what not and the only thing it needs to know about the method is its signature.
    Last edited by Edneeis; Feb 25th, 2004 at 03:48 PM.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Thank you very much! I love this forum, quick and excellent responses!

    The main reason that Im doing this stuff is that in studying for the first test towards an MCAD, and the first chapter in my book deals with events on asp pages. SInce I have NEVER used custom made delegates and events in my programming before, I felt it was needed to dig into this a bit more... hence the crazy example of bell and piccolo. Now I understand it, but I am still unsure when it is really needed to make events like this, I could probably do the same by method calls from bell object to piccolo object...

    kind regards
    Henrik - want to be an mcad

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Perhaps you can answer this one...

    I am playing with a webform and user control
    The problem is that the webusercontrol is performing it's Load stuff before the webforms Load

    So, I solved it like this

    I created an event in the webusercontrol as well as an eventhandler that is performing the "update".

    I raise this event when the webform load is complete

    dim myuc as new WebUserControl1

    myuc.OnAdd()


    The event raises okay and it jumps to the eventhandler in the webusercontrol, BUT in the webusercontrol method I need a value from Session("value"), but I get an exception thete stating "object refrence not set to an instance of an object"

    Why can't I access the session when I am triggering the method by and event? When I do it without events it works fine...

    Anyone want to explain?

    Kind regards
    Henrik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width