Converting type from base class to inherited class

N

Nick Foster

I have created an abstract base class and several classes that inherit it.
i.e.

Public MustInherit Class Person
Public Property Name
......
End Property

Public MustOverride Sub Add()
End Class

Public Class Employee
Inherits Person

Public Property Salary
......
End Property

Public Overrides Sub Add()
....
End Sub
End Class

Public Class Customer
Inherits Person

Public Property LastOrder
.......
End Property

Public Overrides Sub Add()
....
End Sub
End Class

I am trying to create a new object and set it's properties in an asp.net
page

Dim bloke as Person

Select Case PersonType
Case 1
bloke = New Employee
bloke.Salary = intSalary
Case 2
bloke = New Customer
bloke.LastOrder = intOrderNumber
End Select

bloke.Name = strName
bloke.Add

When I try to compile this it fails, telling me that Salary is not a member
of Person even though at runtime the behaviour of bloke would be as an
Employee. Is there a way to get to the Salary property of the Employee
class?

Thanks,
Nick
 
J

Jan Tielens

Try casting the object to the desired type:
CType(bloke, Employee).Salary = intSalary
 
N

Nick Foster

Thank you! I had tried

bloke = CType(bloke,Employee)
bloke.Salary = intSalary

and that hadn't worked so I had been looking for another way.

Thanks again,
Nick
 
B

Bill Borg

Agreed that you need to do the cast, since you DIM'ed
bloke as the generic person and the compiler can't know
that it's the more specific employee.

Other options are to dim these two separately (although
it looks like you are not doing that on purpose for some
reason), or to create Salary as overridable up in person
and then just ignore it for customer types.

Finally, you have to decide whether the employee and
customer animals are all that similar anyway--it's
possible you're going to jump through hoops to get them
to work this way, when maybe they're too different to
bother, and what you really want is rather than customer
and employee sharing a lineage that they instead share a
common structure for the basics like name/address, etc.

hth,

Bill Borg
 
N

Nick Foster

Bill,

The example I gave is a very simplified version of the actual code. The
abstract base has about 20 or so properties that the 8 slightly different
implementation classes share and they are only different by a couple of
properties and methods. The problem is I dont want Customer to expose the
Salary property or Employee to expose LastOrder and I can't see a way in
vb.net to create a kind of optional-to-inherit property in the abstract
class.

Cheers,
Nick
 
J

Jay B. Harlow [MVP - Outlook]

Nick,
In addition to the others comments.

Rather then setting properties on the bloke object, have you considered
passing them as parameters to the constructor? In this case it simplifies
your code immensely!
Dim bloke as Person

Select Case PersonType
Case 1
bloke = New Employee(strName, intSalary)
Case 2
bloke = New Customer(strName, intOrderNumber)
End Select

bloke.Add
Public MustInherit Class Person

' this is protected as only derived classes can call it,
' as the class is abstract (MustInherit)
Protected Sub New(name As String)
Me.Name = name
End Sub
Public Property Name
......
End Property
Public Class Employee
Inherits Person
Public Sub New(name As String, salary As Integer)
MyBase.New(name)
me.Salary = salary
End Sub
Public Property Salary
......
End Property

Hope this helps
Jay
 

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