Events in derived class

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
 
O

One Handed Man \( OHM - Terry Burns \)

Here is an example i knocked up.

Public Class Base

Public Event BaseEvent(ByVal o As Object, ByVal e As EventArgs)
Private m_Text As String = ""


Public Property text() As String
Get
Return m_text
End Get
Set(ByVal Value As String)
If m_Text <> Value Then RaiseEvent BaseEvent(Me,
EventArgs.Empty)
m_Text = Value
End Set
End Property

End Class

Public Class SubClass
Inherits Base



End Class

// IN THE FORM CLASS

Private WithEvents s As New SubClass

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

s.text = "Hello"

End Sub

Private Sub HandleMyEvent(ByVal o As Object, ByVal e As EventArgs)
Handles s.BaseEvent
MessageBox.Show("Event Raised")
End Sub

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (DotNetShadow) scripsit:
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)

'l1' and 'l2' are different objects, so adding handlers to one of them
won't add them to the other object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top