D
DotNetShadow
Hi Guys,
I'm trying to work out how events work in VB.NET Basically I want to
create a base class that has an Event. I would like all derived
classes to inherit this event. I sorta worked out how to do that but
the real problem I have is that If I have a base class with an event
and derived class 1 and 2 inherit this event. Say derived class 1
creates a new derived class 2 how does this new class 2 get the same
event as derived class 1 without adding handlers each time. The
program below works only because I add the addhandler each time, is
this the correct way of doing it ?
Ideally I would like to have a system where I have an event in the
base class that raises an event on the UI thread and any derived
classes just call the base class event handler. It seems the code
below won't work if I don't have the addhandler statements at each
step
Level 2 code has this statement:
AddHandler l2.UpdateHandler, AddressOf MyBase.Update
without it I would never get the messagebox("level 2") to show even
though the event does fire.
Can someone please help?
MAIN FORM
=========
Public Sub StartTesting()
Dim l1 As New Level1
Dim l2 As New Level2
AddHandler l1.UpdateHandler, AddressOf RealUpdate <==== (* ADD
HANDLER)
Dim t1 As New System.Threading.Thread(AddressOf
l1.SpawnThreads)
t1.Start()
End Sub
Public Sub RealUpdate(ByVal text As String)
MsgBox(text)
End Sub
BASE CLASS
==========
Public MustInherit Class ThreadedClass
Protected Sub RaiseDataChanged(ByVal text As String)
'RaiseEvent UpdateHandler(text)
'Update(text)
RaiseEvent UpdateHandler(text)
End Sub
' Event Handler (Update)
Public Event UpdateHandler(ByVal text As String)
Private __updateText As String = "Base"
Public Property updateText() As String
Get
Return __updateText
End Get
Set(ByVal Value As String)
__updateText = Value
End Set
End Property
' Update Method raising event
Public Sub Update(ByVal text As String)
RaiseEvent UpdateHandler(text)
End Sub
End Class
' Level 1 (inherited)
=====================
Public Class Level1
Inherits ThreadedClass
Public Sub SpawnThreads()
Dim l2 As New Level2
l2.RunningProcess()
AddHandler l2.UpdateHandler, AddressOf MyBase.Update
Dim t1 As New System.Threading.Thread(AddressOf
l2.RunningProcess)
t1.Start()
RaiseDataChanged("Level2")
End Sub
End Class
' Level 1 (inherited)
=====================
Public Class Level2
Inherits ThreadedClass
Public Sub RunningProcess()
RaiseDataChanged("Level2")
End Sub
End Class
Regards Dotnetshadow
I'm trying to work out how events work in VB.NET Basically I want to
create a base class that has an Event. I would like all derived
classes to inherit this event. I sorta worked out how to do that but
the real problem I have is that If I have a base class with an event
and derived class 1 and 2 inherit this event. Say derived class 1
creates a new derived class 2 how does this new class 2 get the same
event as derived class 1 without adding handlers each time. The
program below works only because I add the addhandler each time, is
this the correct way of doing it ?
Ideally I would like to have a system where I have an event in the
base class that raises an event on the UI thread and any derived
classes just call the base class event handler. It seems the code
below won't work if I don't have the addhandler statements at each
step
Level 2 code has this statement:
AddHandler l2.UpdateHandler, AddressOf MyBase.Update
without it I would never get the messagebox("level 2") to show even
though the event does fire.
Can someone please help?
MAIN FORM
=========
Public Sub StartTesting()
Dim l1 As New Level1
Dim l2 As New Level2
AddHandler l1.UpdateHandler, AddressOf RealUpdate <==== (* ADD
HANDLER)
Dim t1 As New System.Threading.Thread(AddressOf
l1.SpawnThreads)
t1.Start()
End Sub
Public Sub RealUpdate(ByVal text As String)
MsgBox(text)
End Sub
BASE CLASS
==========
Public MustInherit Class ThreadedClass
Protected Sub RaiseDataChanged(ByVal text As String)
'RaiseEvent UpdateHandler(text)
'Update(text)
RaiseEvent UpdateHandler(text)
End Sub
' Event Handler (Update)
Public Event UpdateHandler(ByVal text As String)
Private __updateText As String = "Base"
Public Property updateText() As String
Get
Return __updateText
End Get
Set(ByVal Value As String)
__updateText = Value
End Set
End Property
' Update Method raising event
Public Sub Update(ByVal text As String)
RaiseEvent UpdateHandler(text)
End Sub
End Class
' Level 1 (inherited)
=====================
Public Class Level1
Inherits ThreadedClass
Public Sub SpawnThreads()
Dim l2 As New Level2
l2.RunningProcess()
AddHandler l2.UpdateHandler, AddressOf MyBase.Update
Dim t1 As New System.Threading.Thread(AddressOf
l2.RunningProcess)
t1.Start()
RaiseDataChanged("Level2")
End Sub
End Class
' Level 1 (inherited)
=====================
Public Class Level2
Inherits ThreadedClass
Public Sub RunningProcess()
RaiseDataChanged("Level2")
End Sub
End Class
Regards Dotnetshadow