Events in inherited classes

C

chetsjunk

I am having problems with an inherited class not handling an event for
the class it is inherited from. But it's not constistent, so I'm
guessing the problem is elsewhere in my code. I created the following
small program to test my problem, and this WORKS FINE:

-----
Class BaseClass
Public Event SaidSomething(ByVal WhatWasSaid As String)
Public Sub Talk(ByVal WhatToSay As String)
MsgBox(WhatToSay)
RaiseEvent SaidSomething(WhatToSay)
End Sub
End Class

Class InheritedClass
Inherits BaseClass
Private Sub InheritedClass_SaidSomething(ByVal WhatWasSaid As
String) Handles MyBase.SaidSomething
MsgBox("You just said '" + WhatWasSaid + ".'")
End Sub
End Class

Module Module1
Sub Main()
Dim obj As New InheritedClass
obj.Talk("Hello world.")
End Sub
End Module
-----

Again, that works fine. But in the code below, the event (sub) is never
invoked by the base class. I try stepping through my code line by line
to verify this and I see it run past the line to raise the event, but
the event code never gets run. Here is the code (I cannot post it all
because there is too much, unfortunately... Way too long)

-----
Public Class objDataObject
Public Event FieldValueChanged(ByVal FieldName As String, ByVal
Value As Object)
Default Public Property FieldValue(ByVal FieldName As String) As
Object
Get
return dr(FieldName)
End Get
Set(ByVal Value As Object)
dr(FieldName) = VBtoDB(Value)
RaiseEvent FieldValueChanged(FieldName, Value)
End Set
End Property
End Class

Public Class objVehicle
Inherits objDataObject
Private Sub objVehicle_FieldValueChanged(ByVal FieldName As String,
ByVal Value As Object) Handles MyBase.FieldValueChanged
msgbox FieldName + " was just changed to " + ctype(value,string)
End Sub
End Class

Module Module1
Sub Main()
Dim obj As New objVehicle
objVehicle.FieldValue("x")="abcd"
End Sub
End Module

-----
When the code in Main runs, the fieldvalue property in the base class is
set, but it never raises the event in the inherited class. I cannot see
a difference between this code and the sample that works, except for a
few thousand lines of code in between, which I don't know how it could
affect.

Any ideas are welcome. Thanks.

Chet
 
C

chetsjunk

I thought perhaps the difference was that I was using a SUB in my test
project and a PROPERTY in my real project. So I changed the sub to a
property in my test project and it still works fine... so there goes
that idea.

Others would be most welcome. Thank you.
 
N

ng

I'm just guessing on this first part, but maybe there's a difference in
using a property and a sub.
The second thing I noticed is a missing variable:
Default Public Property FieldValue(ByVal FieldName As String) As Object
Get
return dr(FieldName)
End Get
Set(ByVal Value As Object)
dr(FieldName) = VBtoDB(Value)
RaiseEvent FieldValueChanged(FieldName, Value)
End Set
End Property

You're trying to pass FieldName and Value, but where is Value in the
list of parameters?

Tom
 

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