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 14 1579
"Mark O'Flynn" <ma*********@oz email.com.au> wrote in
news:DI******** ********@nnrp1. ozemail.com.au: 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.
I think you'll want to declare your manager phone property as "Shadows"
phone. This allows you to create an identically named proprety called
phone which won't override the base class property:
Shadows
Optional. Indicates that this property shadows an identically named
programming element, or set of overloaded elements, in a base class. You
can shadow any kind of declared element with any other kind. If you
shadow a property with another property, the arguments and the return
type do not have to match those in the base class property. A shadowed
element is unavailable from within the derived class that shadows it,
unless the shadowing element is inaccessible, for example if it is
Private.
Note You cannot specify both Overloads and Shadows in the same
property declaration.
To access the bass class data you'll need to provide a property to
access the bass class object (i.e. GetParentObject which will return
Mybase) or provide a property to access the bass class property (i.e.
GetPersonPhone) .
MyBase
The MyBase keyword behaves like an object variable referring to the base
class of the current instance of a class. MyBase is commonly used to
access base class members that are overridden or shadowed in a derived
class. In particular, MyBase.New is used to explicitly call a base class
constructor from a derived class constructor.
It is invalid to use MyBase to call MustOverride base methods.
These are just ideas on the top of my head... and are untested. Maybe
someone with more OO experience can provide a better solution.
--
Lucas Tam (RE********@rog ers.com)
Please delete "REMOVE" from the e-mail address when replying. http://members.ebay.com/aboutme/coolspot18/
"Mark O'Flynn" <ma*********@oz email.com.au> wrote 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).
Where does it fall short? If Manager inherits from Person, then the Msgbox
should show the Phone number.
If it fails on Persons.Find() it may be because that function returns a Person
object instead of a manager object. If that is the case you might add a Shared
function (FromPerson) to the manager class to convert a person object to a
manager object, or you might overload your constructor to allow a person
object to initialize the new manager object.
The above were listed in my own order of preference....
HTH
LFS Where does it fall short? If Manager inherits from Person, then the Msgbox should show the Phone number.
It falls short when Polymorphically using a manager as a Person. Change the managers phone number
using the design he has and it will not be reflected in the *Base* person and vice versa because
essentially
all he has done is create a shallow copy.
If it fails on Persons.Find() it may be because that function returns a Person object instead of a manager object.
It fails because you cant use a widening conversion to convert a Person to a Manager.
A Person class has no concept of a Manager class because it is sub-Person.
Sometimes an author etc will talk about the OOP following real world objects. It might seem like a
natural
class heirarchy to have Manager descend from Person but sometimes this just turns out to be a real
world
illusion tht confuses the design concept. Sometime the distinct is better made with a simple
attribute/property.
What behavioural differences are there between Person and Manager?
Richard
Hi Richard, Sometimes an author etc will talk about the OOP following real world objects. It might seem like a natural class heirarchy to have Manager descend from Person but sometimes this just turns out to be a real world illusion tht confuses the design concept.
LOL! That's a statement and a half! I like it.
Nick.
> LOL! That's a statement and a half! I like it.
Nobody laughs when im trying to be funny..... and then everyone laughs when im being serious.
....What gives?
;)
Richard
"Richard Myers" <fa**@address.c om> wrote It falls short when Polymorphically using a manager as a Person. Change the managers phone number using the design he has and it will not be reflected in the *Base* person and vice versa because essentially all he has done is create a shallow copy.
Evidently you it read a bit different than I:
I would like some advice regarding implementing inheritance using vb.net.
<...> 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.
From that it appears Manager inherits from Person so every method and property
of Person should be available as methods and properties of the Manager object.
Where it got confused was:
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.
So is it better to use a Manager with an embedded Person object, or just
use the Manager object outright? I'd suggest he should be using the manager
objects instead of the person objects, for those people who are managers.
LFS
> Evidently you it read a bit different than I:
Yep. I took this statement If Phone is changed in the Person object, this will not be reflected in the
Manager object.
to be his major concern. I wonder if his "Inheritenc e Design Issue" is whether he should be using
inheritence at all. Maybe the distinction is better made through interfaces?
So is it better to use a Manager with an embedded Person object, or just use the Manager object outright? I'd suggest he should be using the manager objects instead of the person objects, for those people who are managers.
Yeah totally. That is after all why you would create a Person\Manager distinction in the first
place.
Really i think we need more info to give better advice. Its the kind of thing you need to look at
his UML for; I mean can a manager also be managed and thus a Team Member as well?
If all you want is qik and dirty and this is just a tutorial application i'd go with Person as a
property
of Manager. Dead simple and allows a change in a disconnected Manager object to be reflected By Ref
to a Person in your tree/list and vice versa.
You could also look at creating property groups such as PersonContactNu mbers/PersonContactAd dress
that
are much less granular than a simple value type such as
Private phone as String
and so reduce the work in shunting Person data around if you find the Person object as a
property is not granular enough.
But if you want the *right* answer then i suspect this will just be the start of a very long thread.
Archictectural threads are however the most interesting.
Richard
Mark,
When you want the full advantage of .Net, why do you than not use the
dataset.
When you find that not nice encapsulated, than you can go for a strongly
typed dataset, what you can generata or build yourself by inheriting the
dataset.
When you do it right than will your strongly typed dataset have all the
aspects from your classes you are now building, however have a better
connection with your database.
When I now read I load all the people from a database into an array list as part of a Persons class
Than I am a little bit suprised, it looks for me if you try to avoid all
benefits from .Net
Just my thought?
Cor
"Mark O'Flynn" <ma*********@oz email.com.au>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
On 2004-11-25, Mark O'Flynn <ma*********@oz email.com.au> wrote: 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.
Actually, you haven't :-). And you're right, this is a fairly common
situation.
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.
Your big mistake is creating a person object, and then trying to upgrade
that person into a manager. You don't want to do that, what you really
want to do is create a manager from the start.
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.
Don't do it that way. Instead, load both tables from the database, then
have some sort of class or function, create either an ordinary person or
a manager, depending on whether you have manager data available. This
is usually called a Factory pattern, or object factory, or class
factory. It's a pretty common pattern, so it has a lot of names to
describe slightly different methods of implementation.
Something like (in pseudocode)...
Public Function CreatePeople(pe rsonTable, managerTable) As ArrayList
For each personRow in personTable
Dim managerRow as DataRow = managerTable.Fi ndById(person.I D)
If Not manager Is Nothing then
_arrayList.Add( New Manager(personR ow, managerRow))
Else
_arrayList.Add( New Person(personRo w)
End If
Next
Return _arrayList
End Function
There's a lot of variations on this, depending on whether you really
want a managers collection, should managers appear in the persons
collection, are there other kinds of persons, etc.
But the basic point is simple: create each object as the appropriate
class. A person is NOT a manager, and so you shouldn't be creating a
person object when you really want a manager. And you should be
creating your objects at a point in time when you don't know what the
"true" class of that object is. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
----------------
|
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
|
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(){}
};
|
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...
|
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?
| |
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
|
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...
|
by: Mark |
last post by:
why doesn't .NET support multiple inheritance?
I think it's so silly!
Cheers,
Mark
|
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
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |