473,804 Members | 3,675 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.UpdateHandle r, AddressOf MyBase.Update
without it I would never get the messagebox("lev el 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.UpdateHandle r, AddressOf RealUpdate <==== (* ADD
HANDLER)

Dim t1 As New System.Threadin g.Thread(Addres sOf
l1.SpawnThreads )
t1.Start()
End Sub
Public Sub RealUpdate(ByVa l text As String)
MsgBox(text)
End Sub
BASE CLASS
==========
Public MustInherit Class ThreadedClass

Protected Sub RaiseDataChange d(ByVal text As String)

'RaiseEvent UpdateHandler(t ext)
'Update(text)
RaiseEvent UpdateHandler(t ext)

End Sub

' Event Handler (Update)
Public Event UpdateHandler(B yVal 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(t ext)
End Sub

End Class
' Level 1 (inherited)
=============== ======
Public Class Level1
Inherits ThreadedClass

Public Sub SpawnThreads()

Dim l2 As New Level2
l2.RunningProce ss()
AddHandler l2.UpdateHandle r, AddressOf MyBase.Update
Dim t1 As New System.Threadin g.Thread(Addres sOf
l2.RunningProce ss)
t1.Start()

RaiseDataChange d("Level2")
End Sub

End Class

' Level 1 (inherited)
=============== ======
Public Class Level2
Inherits ThreadedClass

Public Sub RunningProcess( )

RaiseDataChange d("Level2")
End Sub
End Class

Regards Dotnetshadow
Nov 20 '05 #1
2 3972
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

s.text = "Hello"

End Sub

Private Sub HandleMyEvent(B yVal 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

"DotNetShad ow" <ro*********@ne tscape.net> wrote in message
news:98******** *************** ***@posting.goo gle.com...
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.UpdateHandle r, AddressOf MyBase.Update
without it I would never get the messagebox("lev el 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.UpdateHandle r, AddressOf RealUpdate <==== (* ADD
HANDLER)

Dim t1 As New System.Threadin g.Thread(Addres sOf
l1.SpawnThreads )
t1.Start()
End Sub
Public Sub RealUpdate(ByVa l text As String)
MsgBox(text)
End Sub
BASE CLASS
==========
Public MustInherit Class ThreadedClass

Protected Sub RaiseDataChange d(ByVal text As String)

'RaiseEvent UpdateHandler(t ext)
'Update(text)
RaiseEvent UpdateHandler(t ext)

End Sub

' Event Handler (Update)
Public Event UpdateHandler(B yVal 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(t ext)
End Sub

End Class
' Level 1 (inherited)
=============== ======
Public Class Level1
Inherits ThreadedClass

Public Sub SpawnThreads()

Dim l2 As New Level2
l2.RunningProce ss()
AddHandler l2.UpdateHandle r, AddressOf MyBase.Update
Dim t1 As New System.Threadin g.Thread(Addres sOf
l2.RunningProce ss)
t1.Start()

RaiseDataChange d("Level2")
End Sub

End Class

' Level 1 (inherited)
=============== ======
Public Class Level2
Inherits ThreadedClass

Public Sub RunningProcess( )

RaiseDataChange d("Level2")
End Sub
End Class

Regards Dotnetshadow

Nov 20 '05 #2
* ro*********@net scape.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.UpdateHandle r, AddressOf MyBase.Update
without it I would never get the messagebox("lev el 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.UpdateHandle r, 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
2267
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
12155
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 events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years namespace MyNamespac public delegate void MyDel() public class MyBase public virtual...
8
2898
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
7
4880
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 subscribe to the event. The problem is when I check go to "fire" the event in ClassA, the event evaluates to null as if no one subscribed.
10
2148
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 some other method? -- Chad Miller President and Director of Software Development Predictive Concepts, Inc. www.predictiveconcepts.com 407.327.9910
4
1351
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 placed: in the Closed event handler, or in the OnClosed override function? What is the difference, and why have the two? When would I use each one?
0
393
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 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...
2
1206
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 Inherit) ShapeObject (Must Inherit) Derived Classes() TestObject (Must Inherite) Derived Classes() Derived Classes()
3
9498
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 state change requires other objects to change as well. The object that sends an event is known as the event sender. The object that receives (or listens for) the event is known as the event receiver. Events implement the "Observer Design...
6
2822
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 imagine, but the base class has properties and methods that are common to all documents. The processing of each docuemnt can result in a range of document specific errors, which I want to raise as events on the document object, but there are...
0
9707
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.