|
-
Feb 25th, 2004, 03:09 PM
#1
Thread Starter
Frenzied Member
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:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyBell As New Bell()
Dim myE As EventArgs
Dim mypic As New Piccolo()
AddHandler MyBell.Ring, AddressOf mypic.Responding_OnRing
MyBell.OnRing(myE)
End Sub
End Class
Public Class Bell
Public Event Ring As EventHandler
Public Overridable Sub OnRing(ByVal e As EventArgs)
RaiseEvent Ring(Me, e)
End Sub
End Class
Public Class Piccolo
Public Sub Responding_OnRing(ByVal sender As Object, ByVal e As EventArgs)
End Sub
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:
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.
-
Feb 25th, 2004, 03:20 PM
#2
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.
-
Feb 25th, 2004, 03:32 PM
#3
Thread Starter
Frenzied Member
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:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyBell As New Bell()
Dim mypic As New Piccolo()
AddHandler MyBell.Ring, AddressOf mypic.Responding_OnRing
AddHandler mypic.Coming, AddressOf Me.Responding_OnComing
MyBell.OnRing(e)
End Sub
Public Sub Responding_OnComing(ByVal sender As Object, ByVal e As EventArgs)
Response.Write("on the way")
End Sub
End Class
Public Class Bell
Public Event Ring As EventHandler
Public Overridable Sub OnRing(ByVal e As EventArgs)
RaiseEvent Ring(Me, e)
End Sub
End Class
Public Class Piccolo
Public Event Coming As EventHandler
Public Sub Responding_OnRing(ByVal sender As Object, ByVal e As EventArgs)
onComing(e)
End Sub
Protected Sub onComing(ByVal e As EventArgs)
RaiseEvent Coming(Me, e)
End Sub
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
-
Feb 25th, 2004, 03:43 PM
#4
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.
-
Feb 26th, 2004, 01:01 AM
#5
Thread Starter
Frenzied Member
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
-
Feb 26th, 2004, 04:05 AM
#6
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|