Inherting classes

T

ThatsIT.net.au

I have some inheritance in my classes
I have Administrator class and a Accounts class then inherit from the Staff
class, which in turn inherits from the Person class

I have a function that finds out if the user is a administrator or in
accounts and returns the correct object, I do not know what that will be so
I can not declare the object accounts or administrator, I have to declare it
as a Person or a Staff object like

dim oUser as Person = getUser(userId)
or
dim oUser as Staff = getUser(userId)

either will work of cause, but I wondering if their was a cost, and if you
should always take the base class or as high up in inheritance as you can.

Thanks
 
N

Nathan Sokalski

It is best to use the most specific class possible in your declaration,
because it means less converting will be involved, as well as leaving less
room for error. In your case, it sounds like this would be the Staff class.
However, for what you are doing I might suggest adding a property to the
Staff class (maybe named "Position" or "Department" or "Title"), and then in
the Administrator and Accounts classes, either set that property in the
constructor or override it so that it returns the desired value. Hopefully
this helps.
 
T

ThatsIT.net.au

The reason I ask is that when I set it as Person the base class, it does so
no problems, but when I set it as Staff more specific, it tells me strict
does not allow implicit conversion and I need to use CType.
 
T

ThatsIT.net.au

well that was with a test class, I re-created then and its no longer asking
for cType, not sure why it was before.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top