473,382 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Events in derived class

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
Nov 20 '05 #1
2 3938
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

"DotNetShadow" <ro*********@netscape.net> wrote in message
news:98**************************@posting.google.c om...
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

Nov 20 '05 #2
* ro*********@netscape.net (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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
8
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
7
by: Opa | last post by:
Hi, I have a class (ClassB) that inherits from another class (ClassA) which has a delegate and event. I can see the events from ClassA in an intance object I create of ClassB. I also...
10
by: Chad Miller | last post by:
I currently have a base form that I inherit. The base for has a custom event. The event will not raise threw the inherited form. I was wondering if events work threw inheritance or should I use...
4
by: Charles Law | last post by:
In a form, there is a set of events that can be handled. There is also a list of (Overrides). So, for the Closed event, for example, there is an override OnClosed. Where should exit code be...
0
by: DotNetShadow | last post by:
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...
2
by: frank | last post by:
Good Morning All, I am working on a CAD application. At the time of this writing I am currently working on the GraphicsEngine. The structure is as follows: GraphicsObject (Base Class, Must...
3
Frinavale
by: Frinavale | last post by:
Background An Event is a message sent out by an object to notify other objects that an action/trigger/state-change (ie. an event) has taken place. Therefore, you should use an event when an object's...
6
by: Charles Law | last post by:
I have a base class and derived classes that relate to a set of documents I process. e.g. DocBase, DocA, DocB, DocC. The processing of each document is handled in teh derived classes, as you might...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.