473,796 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance design issue

I would like some advice regarding implementing inheritance using vb.net.

I have an application written in vb6 that I am rewritting from the ground up
in vb.net to take full advantage of .net, but I have encountered a design
issue implementing inheritance.

To explain my question, I will use an example. I have a Person class, with
properties such as ID, Name, Phone, Address etc. I then have a Manager
class that inherits Person and adds additional properties such as Division,
etc.

I load all the people from a database into an array list as part of a
Persons class.

Now, I want to load the Managers from a table that contains their ID, create
the Manager object with a reference to the Person data, and then load the
manager specific detail.

eg.

Dim oManager as New Manager
oManager = Persons.Find(ID :=1234)
oManager.Divisi on = ...
....
Managers.Add(oM anager)
MsgBox(oManager .Phone.ToString ) ' Show phone number from the base Person
object

Now, this doesnt work as I get an invalid cast exception (as expected).

What I dont want is to create a new Manager, then load it with the data from
the Person Object

eg.

Dim oPerson as Person = Persons.Find(ID :=1234)
Dim oManager as New Manager
With oManager
.ID = oPerson.ID
.Name = oPerson.Name
...
.Division = ...
End With
MsgBox(oManager .Phone.ToString ) ' Show phone number from the Manager object

If Phone is changed in the Person object, this will not be reflected in the
Manager object.

It seems to me that this would be a common situation faces in OOP, so any
advice on how I can get an inheritied object to maintain a reference to the
base object's data, or how best to design a solution would be most
appriciated.

Thanks in advance,

Mark O'Flynn

Nov 21 '05
14 1580
<David>
<..>

I agree with all those points...except i dont see how you can say:
A person is NOT a manager, and so you shouldn't be creating a
person object when you really want a manager.


In his problem space a Manager might need to be a person....
or at least be personable.

For instance if he has a contact group. He may need to add several
different types
of people/roles to the group so in order to keep the contact group
interface
simple he may need a Person base class. He might be better off
combining the factory method with having all his people implement a Person
interface if he needs to group them in any way.

Go for interface based polymorphism rather than inheritence based.

Richard

Nov 21 '05 #11
Nak
> Go for interface based polymorphism rather than inheritence based.

The perfect solution for his needs. Or even as a bodge why not just have an
enumerated type for a person class stating what type of person it is.

Public enum personType
OfficeJunior = 1
Skiv = 2
Manager = 4
BossesPet = 8
Boss = 16
End enum

Could even be a bitwise combination, I've certainly known of it in real
world examples!

Nick.
Nov 21 '05 #12
Richard,

How many times did you already made code like this (this is just a sample as
you never would do it by the way)

\\\
Dim mycontrol As Control = ListView1
If TypeOf mycontrol Is ListView Then
DirectCast(myco ntrol, ListView).Multi Select = True
End If
///

The listview is a manager and the Control describes persons

Just as idea

Cor

"Richard Myers" <fa**@address.c om>
..
<David>
<..>

I agree with all those points...except i dont see how you can say:
A person is NOT a manager, and so you shouldn't be creating a
person object when you really want a manager.


In his problem space a Manager might need to be a person....
or at least be personable.

For instance if he has a contact group. He may need to add several
different types
of people/roles to the group so in order to keep the contact group
interface
simple he may need a Person base class. He might be better off
combining the factory method with having all his people implement a Person
interface if he needs to group them in any way.

Go for interface based polymorphism rather than inheritence based.

Richard

Nov 21 '05 #13
On 2004-11-27, Richard Myers <fa**@address.c om> wrote:
<David>
<..>

I agree with all those points...except i dont see how you can say:
A person is NOT a manager, and so you shouldn't be creating a
person object when you really want a manager.
In his problem space a Manager might need to be a person....
or at least be personable.


Well, a manager IS-A person. It's the opposite that isn't true.

Looking back, perhaps I wasn't clear about that. I think the fact that
manager derives from person is fine. That's what I meant by saying that
there really wasn't an inheritance design problem here. In my example I
still envisioned Manager deriving from Person.
For instance if he has a contact group. He may need to add several
different types of people/roles to the group so in order to keep the
contact group interface simple he may need a Person base class. He
might be better off combining the factory method with having all his
people implement a Person interface if he needs to group them in any
way.

Go for interface based polymorphism rather than inheritence based.


Interface vs. inheritance is an interesting question, but doesn't
address the basic issue of the OP. You can't add an interface to an
object at runtime any easier than you can change the class of an object.

Nov 21 '05 #14
There has been a lot of advice ... thanks to all for your input. My
solution probably resides in a combination of advices.

At the core of my concern was that I expected I could use inheritance to
inherit the 'data' and the 'functionality' . That is, the manager inherits
the persons functions like CalculatePay, CalculateTimeOf f etc, and also
inherit the data, like Name=Mark, Phone=12345 etc (not the property dec but
the referenced data) .

I don't want to implement a Person Interface in my Manager class as I have
to rewrite (copy/paste) the bodies of all functions and properties. The vb6
app worked this way and it has become a maintenance problem. There are many
classes that implemented the Person interface and each one had to copy the
same code to perform the same functions. Also, inheritance is giving me
some other advantages, so I would like to stay with it if I can.
Your inheritance structure is fine here. What you really have is an
object creation problem, which stems from a common situation where the
database structure doesn't map to the class structure very well.


This is correct. The database structure is infact text files used by
another legacy system, and it was not built to map to a class structured
application. This is one constraint that I have.

At this point I am leaning towards passing the Person object into the
Manager constructor. it means that I have to shadow the properties and
functions of the base class, but I can pass the execution to the base class
using mybase.calc.... etc

Anyway, now to implementing some of the suggestions.

Thanks to all for the help.

MOF

Nov 21 '05 #15

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

Similar topics

9
1721
by: Axel Dahmen | last post by:
Hi, when I'm programming style sheets for a whole web application I'm missing a CSS feature to be able to reference CSS properties of other elements. Let's say there is a background/foreground colour combination of: BasicStyles.css: ----------------
9
2377
by: Pedro | last post by:
Hi, all. I'm having a problem which vb developers had for certain al least once. I have my base form, declared as musthinherit; Public MustInherit Class frmsdvPrincipal
19
3196
by: Mike Tyka | last post by:
Hello community, i'm fairly new to using the STL but i've been experimenting a bit with it. I tried to derive a new class say MyString from string like so: class MyString: public string{ public: MyString(){} };
14
12921
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at all obvious in VBA which lacks inheritance. I'm trying the explanation again now. I often find cases where a limited form of inheritance would eliminate duplication my code that seems impossible to eliminate otherwise. I'm getting very...
22
23386
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
7206
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
6
2102
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself so, cannot be called a WebForm itself, the child pages will implement forms). I created a Master.aspx page and removed all HTML from it, added some code to the .aspx.vb file to add controls to my page. Then I created a Child.aspx and changed the...
47
3653
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
4946
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
0
9673
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
9525
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
10452
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
10221
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
10003
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
9050
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...
0
6785
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
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.