|
-
Aug 31st, 2009, 08:22 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Raise Event question..
Hello,
Right now I have some questions regarding how to raise an event... Let's say I have a class that does some calculation using a For... Next loop, from 0 to 100... How would I return the value to the main form AFTER each iteration is completed? How would I raise the event? I have read the documentation but I can't wrap my head around this...
Any help is greatly appreciated!
Thanks in advance!
-
Aug 31st, 2009, 08:53 PM
#2
Re: Raise Event question..
I'm making some assumptions here but if anything doesn't fit your circumstances exactly you can ask about it later.
vb.net Code:
Public Class Form1 Private WithEvents myLooper As Looper Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.myLooper = New Looper Me.myLooper.PerformLoop() End Sub Private Sub myLooper_IterationComplete(ByVal sender As Object, _ ByVal e As IterationCompleteEventArgs) Handles myLooper.IterationComplete Console.WriteLine("Iteration {0} completed.", e.Count) End Sub End Class Public Class IterationCompleteEventArgs Inherits EventArgs Private _count As Integer Public ReadOnly Property Count() As Integer Get Return Me._count End Get End Property Public Sub New(ByVal count As Integer) Me._count = count End Sub End Class Public Class Looper Public Event IterationComplete As EventHandler(Of IterationCompleteEventArgs) Public Sub PerformLoop() For count = 0 To 100 'Do something here. Me.OnIterationComplete(New IterationCompleteEventArgs(count)) Next End Sub Protected Overridable Sub OnIterationComplete(ByVal e As IterationCompleteEventArgs) RaiseEvent IterationComplete(Me, e) End Sub End Class
-
Aug 31st, 2009, 08:57 PM
#3
Thread Starter
Fanatic Member
Re: Raise Event question..
Thanks John! Now I know what I have to do! I'll post back with specific questions!
-
Aug 31st, 2009, 10:16 PM
#4
Thread Starter
Fanatic Member
Re: Raise Event question..
John, I have it now, thanks! No need for questions, your example was just the jump start kick I needed.
-
Aug 31st, 2009, 10:18 PM
#5
Re: [RESOLVED] Raise Event question..
Always happy to give someone a kick.
-
Aug 31st, 2009, 11:10 PM
#6
Re: [RESOLVED] Raise Event question..
I did not know generics could have constraints. (Referring to the EventHandler) That's pretty cool!
-
Aug 31st, 2009, 11:39 PM
#7
Re: [RESOLVED] Raise Event question..
 Originally Posted by ForumAccount
I did not know generics could have constraints. (Referring to the EventHandler) That's pretty cool!
While generics can have constraints, this is not an example of it. There are simply two different EventHandler delegates: one standard and one generic.
http://msdn.microsoft.com/en-us/libr...nthandler.aspx
http://msdn.microsoft.com/en-us/library/db0etb8x.aspx
If you want an example of generic constraints then check this out.
-
Aug 31st, 2009, 11:50 PM
#8
Re: [RESOLVED] Raise Event question..
The EventHandler(Of TEventArgs) is constrained to a base type of System.EventArgs, that's what I was pointing out (more or less).
-
Aug 31st, 2009, 11:54 PM
#9
Re: [RESOLVED] Raise Event question..
 Originally Posted by ForumAccount
The EventHandler(Of TEventArgs) is constrained to a base type of System.EventArgs, that's what I was pointing out (more or less).
D'oh! Of course you were. I hadn't even considered that that generic type was constrained, even though I provided a link to the doco page that defines it. I guess I just never considered using a type that didn't inherit EventArgs so I didn't consider that you couldn't if you wanted to. Good old human brain, filling in the gaps with general fuzziness that we just accept.
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
|