Creating a subclass instance from a base class instance

L

Larry Lard

[formerly posted to ms.public.dotnet.vb.general, but this seems to be a
much higher traffic group]

Hi,

This seems to me like a problem that has a simple solution but I can't
for the life of me work it out.

Suppose I have a base class Employee

Public Class Employee
Public FullName As String
....
End Class

and from it I define the derived subclass Supervisor

Public Class Supervisor
Inherits Employee

Public MaxTeamMembers As Integer
....
End Class


Suppose now I have an employee

Dim e As Employee

who has been made a supervisor. I want to create a Supervisor object s
with all the Employee properties copied from e. I thought of making a
constructor like this in Supervisor:

Public Sub New(fromEmployee As Employee)
MyBase().New

Me.FullName = fromEmployee.FullName
End Sub

but that would break if Employee was ever changed to have more
properties (also in my real app the base class has LOTS of properties).
I thought I might be able to do something like

Public Sub New(fromEmployee As Employee)
MyBase().New

MyBase = fromEmployee ' oh no you can't
End Sub

but I can't. The very first thing I tried was just casting an As
Employee variable to Supervisor, but it wouldn't let me - after some
thought I now understand that it is correct.

What is the proper way to do what I want?
Thanks,
 
K

Ken Tucker [MVP]

Hi,

Public Class Employee

Public FullName As String

End Class

Public Class Supervisor

Inherits Employee

Public Sub New(ByVal E As Employee)

Me.FullName = E.FullName

End Sub

Public MaxTeamMembers As Integer

End Class



Ken

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

[formerly posted to ms.public.dotnet.vb.general, but this seems to be a
much higher traffic group]

Hi,

This seems to me like a problem that has a simple solution but I can't
for the life of me work it out.

Suppose I have a base class Employee

Public Class Employee
Public FullName As String
....
End Class

and from it I define the derived subclass Supervisor

Public Class Supervisor
Inherits Employee

Public MaxTeamMembers As Integer
....
End Class


Suppose now I have an employee

Dim e As Employee

who has been made a supervisor. I want to create a Supervisor object s
with all the Employee properties copied from e. I thought of making a
constructor like this in Supervisor:

Public Sub New(fromEmployee As Employee)
MyBase().New

Me.FullName = fromEmployee.FullName
End Sub

but that would break if Employee was ever changed to have more
properties (also in my real app the base class has LOTS of properties).
I thought I might be able to do something like

Public Sub New(fromEmployee As Employee)
MyBase().New

MyBase = fromEmployee ' oh no you can't
End Sub

but I can't. The very first thing I tried was just casting an As
Employee variable to Supervisor, but it wouldn't let me - after some
thought I now understand that it is correct.

What is the proper way to do what I want?
Thanks,
 
L

Larry Lard

Ken Tucker [MVP] wrote:
[snip]
Public Sub New(ByVal E As Employee)

Me.FullName = E.FullName

End Sub

So no neat way to automagically populate the base class members in the
subclass object from the given base class object? I was trying to avoid
having to create and maintain a list like

Me.prop1 = E.prop1
Me.prop2 = E.prop2
Me.prop3 = E.prop3
Me.prop4 = E.prop4
' ...

but if that is the only way (without using Reflection, which just looks
horrible) then it must do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top