473,799 Members | 2,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance and Interfaces

I originally posted this in the WindowsForms group but that may not
have been the best place to post it. So, I am reposting here.

--------------------------------------

I am trying to implement the new INotifyProperty Changed interface in my
custom business object. The problem I'm having is with the inheritance
model.

I have a base object called "Person". The object "Employee" inherits
from person and provides additional properties and methods.

I implemented INotifyProperty Changed in the Person base class and
everything worked great. However, how do I implement it in the derived
"Employee" class.

If I implement it the same way, the compiler warns that:

"event 'PropertyChange d' conflicts with event 'PropertyChange d' in the
base class 'Person' and should be declared 'Shadows'."

OK. So I change the event definition to "Shadows". Then, I still get
the following warning:

"'System.Compon entModel.INotif yPropertyChange d.PropertyChang ed' is
already implemented by the base class 'Common.Person' .
Re-implementation of event assumed."

If I ignore BOTH warnings and run it, it works fine. If I designate
the event as "Shadows" and run it, it works fine.

Am I doing this correctly or am I in high weeds?

Any help would be greatly appreciated.

Sincerely,
Glen Wolinsky

Nov 23 '05 #1
15 1639
Glen,
You only need to implement INotifyProperty Changed in the base class, then
allow derived classes to raise the event. The "standard" pattern is to do
something like:

Public Class Person
Implements System.Componen tModel.INotifyP ropertyChanged

Public Event PropertyChanged (ByVal sender As Object, ByVal e As
System.Componen tModel.Property ChangedEventArg s) Implements
System.Componen tModel.INotifyP ropertyChanged. PropertyChanged

Protected Overridable Sub OnPropertyChang ed(ByVal e As
System.Componen tModel.Property ChangedEventArg s)
RaiseEvent PropertyChanged (Me, e)
End Sub

Private m_name As String

Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
Dim e As New
System.Componen tModel.Property ChangedEventArg s("Name")
OnPropertyChang ed(e)
End Set
End Property

End Class

Public Class Employee
Inherits Person

Private m_salary As String

Public Property Salary() As String
Get
Return m_salary
End Get
Set(ByVal value As String)
m_salary = value
Dim e As New
System.Componen tModel.Property ChangedEventArg s("Salary")
OnPropertyChang ed(e)
End Set
End Property

End Class

The Overridable Sub OnPropertyChang ed allows derived classes to raise the
event, alternatively derived class can override the method & offer extra
processing when the event is raised...

For details on the above pattern see:

http://msdn.microsoft.com/library/de...Guidelines.asp

http://msdn.microsoft.com/library/de...Guidelines.asp

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
<gw*******@mill ermartin.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
|I originally posted this in the WindowsForms group but that may not
| have been the best place to post it. So, I am reposting here.
|
| --------------------------------------
|
| I am trying to implement the new INotifyProperty Changed interface in my
| custom business object. The problem I'm having is with the inheritance
| model.
|
| I have a base object called "Person". The object "Employee" inherits
| from person and provides additional properties and methods.
|
| I implemented INotifyProperty Changed in the Person base class and
| everything worked great. However, how do I implement it in the derived
| "Employee" class.
|
| If I implement it the same way, the compiler warns that:
|
| "event 'PropertyChange d' conflicts with event 'PropertyChange d' in the
| base class 'Person' and should be declared 'Shadows'."
|
| OK. So I change the event definition to "Shadows". Then, I still get
| the following warning:
|
| "'System.Compon entModel.INotif yPropertyChange d.PropertyChang ed' is
| already implemented by the base class 'Common.Person' .
| Re-implementation of event assumed."
|
| If I ignore BOTH warnings and run it, it works fine. If I designate
| the event as "Shadows" and run it, it works fine.
|
| Am I doing this correctly or am I in high weeds?
|
| Any help would be greatly appreciated.
|
| Sincerely,
| Glen Wolinsky
|
Nov 23 '05 #2
Jay,

Talk about having a "duh" moment. That works perfectly. I REALLY don't
know why I didn't think of it.

However, at the risk of repeating the "duh" offense, I am also trying to
implement the IDataErrorInfo interface. The difference here is that the
items I'm dealing with are not methods as in the INotifyProperty Changed
interface. IDataErrorInfo implements two properties. I'm running into
the same problem as described previously.

Do you have another bit of charity for this VERY tired amateur? ;o)

Thanks,
Glen

"Jay B. Harlow [MVP - Outlook]" wrote:
Glen,
You only need to implement INotifyProperty Changed in the base class, thenallow derived classes to raise the event. The "standard" pattern is to dosomething like:

Public Class Person
Implements System.Componen tModel.INotifyP ropertyChanged

Public Event PropertyChanged (ByVal sender As Object, ByVal e As
System.Compone ntModel.Propert yChangedEventAr gs) Implements
System.Compone ntModel.INotify PropertyChanged .PropertyChange d

Protected Overridable Sub OnPropertyChang ed(ByVal e As
System.Compone ntModel.Propert yChangedEventAr gs)
RaiseEvent PropertyChanged (Me, e)
End Sub

Private m_name As String

Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
Dim e As New
System.Compone ntModel.Propert yChangedEventAr gs("Name")
OnPropertyChang ed(e)
End Set
End Property

End Class

Public Class Employee
Inherits Person

Private m_salary As String

Public Property Salary() As String
Get
Return m_salary
End Get
Set(ByVal value As String)
m_salary = value
Dim e As New
System.Compone ntModel.Propert yChangedEventAr gs("Salary")
OnPropertyChang ed(e)
End Set
End Property

End Class

The Overridable Sub OnPropertyChang ed allows derived classes to raise theevent, alternatively derived class can override the method & offer extraprocessing when the event is raised...

For details on the above pattern see:

http://msdn.microsoft.com/library/de...y/en-us/cpgenr ef/html/cpconEventNamin gGuidelines.asp
http://msdn.microsoft.com/library/de...y/en-us/cpgenr ef/html/cpconEventUsage Guidelines.asp
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
<gw*******@mil lermartin.com> wrote in message
news:11******* **************@ g49g2000cwa.goo glegroups.com.. .
|I originally posted this in the WindowsForms group but that may not
| have been the best place to post it. So, I am reposting here.
|
| --------------------------------------
|
| I am trying to implement the new INotifyProperty Changed interface in my| custom business object. The problem I'm having is with the inheritance| model.
|
| I have a base object called "Person". The object "Employee" inherits
| from person and provides additional properties and methods.
|
| I implemented INotifyProperty Changed in the Person base class and
| everything worked great. However, how do I implement it in the derived| "Employee" class.
|
| If I implement it the same way, the compiler warns that:
|
| "event 'PropertyChange d' conflicts with event 'PropertyChange d' in the| base class 'Person' and should be declared 'Shadows'."
|
| OK. So I change the event definition to "Shadows". Then, I still get| the following warning:
|
| "'System.Compon entModel.INotif yPropertyChange d.PropertyChang ed' is
| already implemented by the base class 'Common.Person' .
| Re-implementation of event assumed."
|
| If I ignore BOTH warnings and run it, it works fine. If I designate
| the event as "Shadows" and run it, it works fine.
|
| Am I doing this correctly or am I in high weeds?
|
| Any help would be greatly appreciated.
|
| Sincerely,
| Glen Wolinsky
|


--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 23 '05 #3
Glen,
I would do something like:

'
' Copyright © 2005, Jay B. Harlow, All Rights Reserved.
'
Option Strict On
Option Explicit On

Imports System.Componen tModel

Public Class Person
Implements INotifyProperty Changed
Implements IDataErrorInfo

Private m_name As String

Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
If value = "" Then
ColumnError("Sa lary") = "Name is blank!"
Else
ColumnError("Sa lary") = ""
End If
m_name = value
Dim e As New PropertyChanged EventArgs("Name ")
OnPropertyChang ed(e)
End Set
End Property

#Region " INotifyProperty Changed support "

Public Event PropertyChanged (ByVal sender As Object, ByVal e As
PropertyChanged EventArgs) Implements INotifyProperty Changed.Propert yChanged

Protected Overridable Sub OnPropertyChang ed(ByVal e As
PropertyChanged EventArgs)
RaiseEvent PropertyChanged (Me, e)
End Sub

#End Region

#Region " IDataErrorInfo support "

Private m_rowError As String
Private m_columnErrors As New
System.Collecti ons.Specialized .StringDictiona ry

Private ReadOnly Property IDataErrorInfo_ Error() As String Implements
IDataErrorInfo. Error
Get
Return m_rowError
End Get
End Property

Private ReadOnly Property IDataErrorInfo_ Item(ByVal columnName As
String) As String Implements IDataErrorInfo. Item
Get
Return m_columnErrors( columnName)
End Get
End Property

Public Property RowError() As String
Get
Return m_rowError
End Get
Set(ByVal value As String)
m_rowError = value
End Set
End Property

Public Property ColumnError(ByV al columnName As String) As String
Get
Return m_columnErrors( columnName)
End Get
Set(ByVal value As String)
m_columnErrors( columnName) = value
End Set
End Property

Public Sub ClearErrors()
m_rowError = ""
m_columnErrors. Clear()
End Sub
#End Region

End Class

Public Class Employee
Inherits Person

Private m_salary As Decimal

Public Property Salary() As Decimal
Get
Return m_salary
End Get
Set(ByVal value As Decimal)
If value < 0 Then
ColumnError("Sa lary") = "Salary less then 0!"
Else
ColumnError("Sa lary") = ""
End If
m_salary = value
Dim e As New PropertyChanged EventArgs("Sala ry")
OnPropertyChang ed(e)
End Set
End Property

End Class

NOTE: I did not add error checking to the ColumnError or RowError
properties...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Glen Wolinsky" <gw*******@mill ermartin.com> wrote in message
news:OV******** ******@TK2MSFTN GP10.phx.gbl...
| Jay,
|
| Talk about having a "duh" moment. That works perfectly. I REALLY don't
| know why I didn't think of it.
|
| However, at the risk of repeating the "duh" offense, I am also trying to
| implement the IDataErrorInfo interface. The difference here is that the
| items I'm dealing with are not methods as in the INotifyProperty Changed
| interface. IDataErrorInfo implements two properties. I'm running into
| the same problem as described previously.
|
| Do you have another bit of charity for this VERY tired amateur? ;o)
|
| Thanks,
| Glen
|
<<snip>>
Nov 23 '05 #4
That did the trick. I made a few modifications for our setup, but your
solution was right on the mark. Thank you SO much for your time and
assistance.

Sincerely,
Glen Wolinsky

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 23 '05 #5
Jay,

One other question: What is your idea for providing an "IsValid"
property to a derived object with collections? This would be so that
the UI could let the object tell it if writting the changes is allowed
or if there are still problems with data.

Again, thanks for your time.

Sincerely,
Glen

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 23 '05 #6
Glen,
I don't follow the question.

I would expect the object to let the UI know if its ok for the UI to tell
the object to save itself. If the UI attempted to save the object & the
object wasn't valid I would expect the object to let the UI know that its
not appropriate to save object at this time.

So yes, a "IsValid" can be appropriate property for an object to have,
however I would expect it would be public read-only & protected writable.

Note you need VB 2005 for public read-only & protected writable properties.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Glen Wolinsky" <gw*******@mill ermartin.com> wrote in message
news:O5******** *****@TK2MSFTNG P15.phx.gbl...
| Jay,
|
| One other question: What is your idea for providing an "IsValid"
| property to a derived object with collections? This would be so that
| the UI could let the object tell it if writting the changes is allowed
| or if there are still problems with data.
|
| Again, thanks for your time.
|
| Sincerely,
| Glen
|
| --
| Sent via .NET Newsgroups
| http://www.dotnetnewsgroups.com
Nov 23 '05 #7
Jay,

Sorry if I wasn't very clear. We're on the same page in regards to
providing an "IsValid" property. My question is to the best way to
provide it.

Your scenario only updates the validity of a property IF the property
was set during its life. It seems to me (unless I'm missing something
-- very possible) that the object couldn't be sure if it's valid unless
it iterated through ALL the property checks when "IsValid" is read.
That would be the only way to ensure that all validations are hit.

Am I on it or off in high weeds?

Below, I am enclosing some code that I had started on this morning after
my last reply. I hope it makes some sense.

Thanks,
Glen
--------------------------------
This goes in my base class:
--------------------------------

Public ReadOnly Property ErrorsList() As Specialized.Str ingDictionary
Get
Return _itemErrors
End Get
End Property
Public MustOverride ReadOnly Property IsValid() As Boolean

Protected MustOverride Sub ValidateItem(By Val ItemName As String)
Protected MustOverride Sub ValidateAllItem s()

--------------------------------
This goes in my derived class:
--------------------------------

Public ReadOnly Property ErrorsList() As Specialized.Str ingDictionary
Get
Return _itemErrors
End Get
End Property

#Region " Validate Item (Override) "
Protected Overrides Sub ValidateItem(By Val ItemName As String)
Select Case ItemName
Case "FirstName"
MyBase.ItemErro rs("FirstName" ) = IIf(MyBase.Firs tName = "", "First
Name is required.", "").ToStrin g
Case "Initials"
MyBase.ItemErro rs("Initials") = IIf(_initials = "", "Initials are
required.", "").ToStrin g
Etc...
Case Else 'Do nothing.
End Select
End Sub
#End Region

#Region " Validate All Items (Override) "
Protected Overrides Sub ValidateAllItem s()
ValidateItem("F irstName")
ValidateItem("I nitials")
Etc.....
End Sub
#End Region

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 23 '05 #8
Glen,
When the object is created it would know if it was valid or not. You can
insure this by using the properties for initialization instead of the
backing fields.

As the user changed properties they would know if the individual properties
were valid or not.

"Invalid" properties would (should) show up in the _itemErrors collection.

IsValid could be (would be!) as simply as "Return _itemErrors.Cou nt = 0"
assuming _itemErrors only contained errors & did not contain successes...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Glen Wolinsky" <gw*******@mill ermartin.com> wrote in message
news:el******** ******@TK2MSFTN GP12.phx.gbl...
| Jay,
|
| Sorry if I wasn't very clear. We're on the same page in regards to
| providing an "IsValid" property. My question is to the best way to
| provide it.
|
| Your scenario only updates the validity of a property IF the property
| was set during its life. It seems to me (unless I'm missing something
| -- very possible) that the object couldn't be sure if it's valid unless
| it iterated through ALL the property checks when "IsValid" is read.
| That would be the only way to ensure that all validations are hit.
|
| Am I on it or off in high weeds?
|
| Below, I am enclosing some code that I had started on this morning after
| my last reply. I hope it makes some sense.
|
| Thanks,
| Glen
|
|
| --------------------------------
| This goes in my base class:
| --------------------------------
|
| Public ReadOnly Property ErrorsList() As Specialized.Str ingDictionary
| Get
| Return _itemErrors
| End Get
| End Property
|
|
| Public MustOverride ReadOnly Property IsValid() As Boolean
|
| Protected MustOverride Sub ValidateItem(By Val ItemName As String)
| Protected MustOverride Sub ValidateAllItem s()
|
| --------------------------------
| This goes in my derived class:
| --------------------------------
|
| Public ReadOnly Property ErrorsList() As Specialized.Str ingDictionary
| Get
| Return _itemErrors
| End Get
| End Property
|
| #Region " Validate Item (Override) "
| Protected Overrides Sub ValidateItem(By Val ItemName As String)
| Select Case ItemName
| Case "FirstName"
| MyBase.ItemErro rs("FirstName" ) = IIf(MyBase.Firs tName = "", "First
| Name is required.", "").ToStrin g
| Case "Initials"
| MyBase.ItemErro rs("Initials") = IIf(_initials = "", "Initials are
| required.", "").ToStrin g
| Etc...
| Case Else 'Do nothing.
| End Select
| End Sub
| #End Region
|
| #Region " Validate All Items (Override) "
| Protected Overrides Sub ValidateAllItem s()
| ValidateItem("F irstName")
| ValidateItem("I nitials")
| Etc.....
| End Sub
| #End Region
|
|
|
| --
| Sent via .NET Newsgroups
| http://www.dotnetnewsgroups.com
Nov 23 '05 #9
Jay,

There are two ways my objects are initialized:

1. If this is a "get" operation, the object loads the employee data from
the database and sets all the backing fields;

2. This is a new employee and the backing fields are initialized during
variable instantiation.

Option 2 should be full of errors since it's empty and there are several
required fields. Option 1 would have to use the properties instead of
the backing fields like you mentioned in order make sure the current
data is valid.

So, in order to be consistent, are you saying I should initialize the
"properties " (not the backing variables) in a subroutine that is called
by both the constructor AND the "Get" routine?

Thanks,
Glen

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 23 '05 #10

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

Similar topics

3
8395
by: Joe Delphi | last post by:
Does Visual Basic support multiple inheritance? That is one child class inheriting from more than one parent class. JD
8
3221
by: Shawn Casey | last post by:
Consider the following code: interface IBase { virtual void BaseFunction() = 0; }; interface IDerived : public IBase { virtual void DerivedFunction() = 0;
12
7060
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns I've ended up using successfully for certain kinds of inheritance and polymorphism, and some that have not worked out so well. Let's start with obvious things that turn out not to work well: 1. Use interface classes and "Implements" for...
22
23388
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
15
7207
by: Sinex | last post by:
Hi, Why does C# disallow multiple inheritance? Whats the reason behind this? Is there any advantage or is it just a method to avoid some problems (if so, what problems?) that come with multiple inheritance? regards, Sinex
7
12878
by: Hazz | last post by:
Are there any good references/articles/books which provide clarity toward my insecurity still on deciding how to model a complex system? I still feel uncomfortable with my understanding, even though I have worked with these systems on when to decide to use interfaces (and how they should be developed) as opposed to or complemented by the use of inheritance from base classes. If I am thinking from the point of view of some specific activity...
0
1197
by: Terry Hancock | last post by:
I've been discussing PyProtocols with a a friend collaborating with me on a SF game project, about the benefits and design concept of "component architecture", and I'm a little confused by what I'm learning. I learned the concepts of "component architecture" from the Zope project, where if I'm not mistaken, "components" are essentially python "mix-in" classes, and you make a class from components by using multiple inheritance and...
23
4616
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
18
1693
by: GD | last post by:
Please remove ability to multiple inheritance in Python 3000. Multiple inheritance is bad for design, rarely used and contains many problems for usual users. Every program can be designed only with single inheritance. I also published this request at http://bugs.python.org/issue2667
3
4845
by: johanatan | last post by:
When I first heard about these new features, I was very excited as it would have (if implemented as I had expected) rendered mimicking multiple inheritance almost painless in C#. Unfortunately, due to a couple limitations of the language, MI is still not attainable (at least not succinctly). 1 - Interfaces cannot define data (only properties) 2 - Extension methods cannot be used to mix in property definitions (or implement interface...
0
10482
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
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...
1
10225
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
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...
1
7564
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
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
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
4139
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
3759
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.