M
Mark O'Flynn
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.Division = ...
....
Managers.Add(oManager)
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
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.Division = ...
....
Managers.Add(oManager)
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