473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ReInstantiate an Object?

Hi,

I don't even know if this can be done..

Lets say I have two objects,
---
Public Class Employee

End Class

Public Class Boss : Inherits Employee

End Class

--

At runtime, I have no control over when an "Employee" is instantiated, but I do have a reference to it. Is there a way to "ReInstantiate" it so it is now a "Boss", but it would keep the state of current employee. I am not after totally redeclaring the object, rather I just want to "addon" to it.

Any ideas?
--Michael
Nov 21 '05 #1
8 3959
"Raterus" <ra*****@hotmail.com> schrieb:
I don't even know if this can be done..

Lets say I have two objects,
---
Public Class Employee

End Class

Public Class Boss : Inherits Employee

End Class

--

At runtime, I have no control over when an "Employee" is instantiated, but
I do have a reference to it. Is there a way to "ReInstantiate" it so it is
now a "Boss", but it would keep the state of current employee. I am not
after totally redeclaring the object, rather I just want to "addon" to it.


You may want to add a shared method to 'Boss' that takes an 'Employee'
object as an argument and returns a corresponding 'Boss' object. In this
method's implementation you can copy property values from the passed object
to a new instance of 'Boss'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
Or (extending on Herfried's suggestion) you could add a new constructor to
the Boss class that took in the Employee instance and whatever other
property values that are "boss-specific".

Dim MyBoss as New Boss(MyEmployee, YearlyBonusAmt, PrivateParkingSpaceNum,
SlushFundAccountNum)

- Mitchell S. Honnert

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Oz**************@TK2MSFTNGP14.phx.gbl...
"Raterus" <ra*****@hotmail.com> schrieb:
I don't even know if this can be done..

Lets say I have two objects,
---
Public Class Employee

End Class

Public Class Boss : Inherits Employee

End Class

--

At runtime, I have no control over when an "Employee" is instantiated, but
I do have a reference to it. Is there a way to "ReInstantiate" it so it
is now a "Boss", but it would keep the state of current employee. I am
not after totally redeclaring the object, rather I just want to "addon" to
it.


You may want to add a shared method to 'Boss' that takes an 'Employee'
object as an argument and returns a corresponding 'Boss' object. In this
method's implementation you can copy property values from the passed
object to a new instance of 'Boss'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
Raterus,
In addition to the other comments.

Rather then have Boss Inherit Employee, I would consider making Boss a Role
that an Employee could have. This allows an employee to change the role they
play. Or even support multiple Roles.

Something like:

Public Class Employee

Public Property Role As EmployeeRole

End Class

Public MustInherit Class EmployeeRole

End Class

Public Class BossRole : Inherits EmployeeRole

End Class

Public Class CeoRole : Inherits EmployeeRole

End Class
Dim person1 As New Employee
person1.Role = new BossRole ' promotion
person1.Role = new CeoRole ' another promotion...

If an employee could have multiple roles, then I would define an
Employee.Roles collection that returned a collection of roles. Alternatively
you could apply the Composite Pattern to Role which would also allow
multiple roles.

For members that the Boss role could "override" in an Employee, I would
define the member in Employee, then delegate to the contained EmployeeRole.

Public Class Employee

Public Property Role As EmployeeRole

Public Function CalculateSalary() As Decimal
Return Role.CalculateSalary()
End Function

End Class

Public MustInherit Class EmployeeRole

Public MustOverride Function CalculateSalary() As Decimal

End Class

Hope this helps
Jay
"Raterus" <ra*****@hotmail.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
Hi,

I don't even know if this can be done..

Lets say I have two objects,
---
Public Class Employee

End Class

Public Class Boss : Inherits Employee

End Class

--

At runtime, I have no control over when an "Employee" is instantiated, but I
do have a reference to it. Is there a way to "ReInstantiate" it so it is
now a "Boss", but it would keep the state of current employee. I am not
after totally redeclaring the object, rather I just want to "addon" to it.

Any ideas?
--Michael
Nov 21 '05 #4
Jay,

A Boss has at least collection of zero or more employee's

I think that with that I have said enough for you what I wanted to say

:-)

Cor
Nov 21 '05 #5
Cor,
| A Boss has at least collection of zero or more employee's
Yes! a Boss can have zero or more employees! This might be a good place for
a Composite Pattern, as there may be other zero to many relationships
between employees, for example paired programming & programming teams. In my
original example I would consider making BossRole contain the collection of
employee's. In the above example I would consider making Employee itself
contain the collection. Depending on the requirements of the project.

| I think that with that I have said enough for you what I wanted to say
No idea what you wanted to say. Or more appropriately I have no idea what
you are trying to add to this thread. As the OP wanted to change an Employee
object into a Boss object. The two common ways of doing this is copying as
Herfried & Mitchell suggested & the Role Pattern that I suggested. I don't
see a direct connection between number of employees & change an object's
type.

Hope this helps
Jay
"Cor Ligthert" <no************@planet.nl> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
| Jay,
|
| A Boss has at least collection of zero or more employee's
|
| I think that with that I have said enough for you what I wanted to say
|
| :-)
|
| Cor
|
|
Nov 21 '05 #6
Jay,

In my opinion does Boss derives from Employees when he has himself Employees
or any other reasons that does distinct him from an employee.

As I understand your solution well than looks it for me the same as giving
feathers to a mammal because more animals have that.

I think that is a very essential point in this thread and for me that what
the OP not want to do.

What in my opinion means that he want to use a Boss object as an Employee.

In code something like this very short and bad written.

\\\
Public Class Test
Public Shared Sub Main()
Dim a As New Employee
a.name = "Jan"
Dim b As Employee
b = New Boss
b.name = "Piet"
DirectCast(b, Boss).Add(a.name)
End Sub
End Class
Public Class Employee
Public name As String
End Class
Public Class Boss : Inherits Employee
Public employees As New ArrayList
Public Sub Add(ByVal emp As String)
employees.Add(emp)
End Sub
End Class
///

Because I saw already so many messages, did I not wanted to add this,
because this is something I am sure of that it does not add much to your
knowledge and thought maybe can you tell that.

However when you don't agree will I without any problem add this to the main
thread.

:-)

Cor
Nov 21 '05 #7
Jay,

It was yesterday. I think that it was that I did not had an answer and
thought maybe you have.

Now that I thinked about it, is the question the same as "Can I make from a
combobox a listbox when I have already finstanted and filled that". Of
course I can do something like that, however than I have to deepclone most
parts and I think that that is not the question. However maybe you have.

Cor
Nov 21 '05 #8
Cor,
| In my opinion does Boss derives from Employees when he has himself
Employees
| or any other reasons that does distinct him from an employee.
I am not disputing that. As I stated, Herfried & Mitchell gave the
inheritance with cloning solution.

A well know OO alternative to Inheritance is the Role Pattern. The Role
Pattern is useful when an object (Employee) needs to change its type (Boss).
Especially when copying & cloning may not be desirable! The "best"
discussion that I know of on the Role Pattern is in Peter Coad's, Mark
Mayfield's, and Jon Kern's book "Java Design - Building Better Apps &
Applets". They discuss this problem & how to implement it with Inheritance &
how to implement it with the Role pattern & why you would choose one over
the other. You can also use google to search for "Role Pattern" (include the
quotes) to find a number of useful links. I would suggest these:

http://www.cs.wustl.edu/~doc/RandD/P...ole-object.pdf
http://wiki.cs.uiuc.edu/AnalysisPatterns/Party

I'm sure there are others, the second link uses the term "actor-role
pattern".
Basically inheritance is useful when you need "is a special kind of" (a Boss
is a special kind of Employee) while the Role Pattern is when you need "is a
role played by" (Boss is a role played by an Employee). So I hope you see
both are applicable here.

I hope you agree one of the major problems with inheriting & cloning an
Employee is when you have many references to that Employee? If you clone it
you will need to change all the references, how do you know what all those
references are? If you implement the Role Pattern you just need to change
the Role property.

Another major problem with inheriting & cloning an Employee is what happens
when the Employee can hold multiple roles? What happens when the employee is
both a Boss & Administrative Assistant? Remember that .net does not allow
multiple inheritance. With the Role pattern Employee could have a
RoleCollection and/or PrimaryRole & SecondaryRole properties. Allowing that
Employee to hold multiple Roles.

Consider the example from the Java Design book:

Class Person - represents a person

Class Agent : Inherits Person - a person who sells airline tickets

Class Passenger : Inherits Person - a person who rides on the plane
Now how do you represent a person riding on a plan who sells Airline
tickets?

Class AgentPassenger : Inherits Agent : Inherits Passenger

Oops: You can only inherit from a single base class.

Hence the use of Roles

Class Person - Represents a person

Class PersonRole - represents the roles that a person can have

Class AgentRole - Inherits PersonRole

Class PassengerRole - Inherits PersonRole

If Person can have 0 to many PersonRole objects, that Person can be both an
Agent & a Passenger.

| As I understand your solution well than looks it for me the same as giving
| feathers to a mammal because more animals have that.
Huh?

Please take the time to fully understand the Role Pattern before commenting
further! Thank you!

Hope this helps
Jay

"Cor Ligthert" <no************@planet.nl> wrote in message
news:O5****************@TK2MSFTNGP14.phx.gbl...
| Jay,
|
| In my opinion does Boss derives from Employees when he has himself
Employees
| or any other reasons that does distinct him from an employee.
|
| As I understand your solution well than looks it for me the same as giving
| feathers to a mammal because more animals have that.
|
| I think that is a very essential point in this thread and for me that what
| the OP not want to do.
|
| What in my opinion means that he want to use a Boss object as an Employee.
|
| In code something like this very short and bad written.
|
| \\\
| Public Class Test
| Public Shared Sub Main()
| Dim a As New Employee
| a.name = "Jan"
| Dim b As Employee
| b = New Boss
| b.name = "Piet"
| DirectCast(b, Boss).Add(a.name)
| End Sub
| End Class
| Public Class Employee
| Public name As String
| End Class
| Public Class Boss : Inherits Employee
| Public employees As New ArrayList
| Public Sub Add(ByVal emp As String)
| employees.Add(emp)
| End Sub
| End Class
| ///
|
| Because I saw already so many messages, did I not wanted to add this,
| because this is something I am sure of that it does not add much to your
| knowledge and thought maybe can you tell that.
|
| However when you don't agree will I without any problem add this to the
main
| thread.
|
| :-)
|
| Cor
|
|
Nov 21 '05 #9

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

Similar topics

6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
15
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
1
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: Nick Zdunic | last post by:
I have a remotable object running in my host application. The host starts up and creates the object. Within a method to start the remote object doing its thing it creates an object. ...
0
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.