Am I part of a derived class?

  • Thread starter Thread starter gwolinsky
  • Start date Start date
G

gwolinsky

Situation:

I have a class (employee). This class can be used alone but is also a
base class for more specific types of employees that require further
data.

The "attorney" class inherits from "employee" and adds some additional
properties.

Question:

Is there a way for the "employee" class instance to look at itself and
tell whether or not is part of a derived "attorney" instance or is a
stand-alone instance?

Any help would be greatly appreciated.

Sincerely,
Glen
 
Situation:

I have a class (employee). This class can be used alone but is also a
base class for more specific types of employees that require further
data.

The "attorney" class inherits from "employee" and adds some additional
properties.

Question:

Is there a way for the "employee" class instance to look at itself and
tell whether or not is part of a derived "attorney" instance or is a
stand-alone instance?

' Suppose emp is an Employee object
If TypeOf emp Is Attorney Then
'emp is also an Attorney
'so we know we can do eg this:
Dim att As Attorney = DirectCast(emp, Attorney)
'now access attorney-specific stuff with att.whatever
ElseIf TypeOf emp Is Engineer Then
'etc
'...
Else
'emp is just a plain Employee
End If
 
Cor Ligthert said:
Glen,

You have made from those classes objects, you should know in your program
what you instance.

You are not dealing with classes, only when you want to know the type
(class) of the object.

For what is gettype.
http://msdn.microsoft.com/library/d...f/html/frlrfsystemobjectclassgettypetopic.asp


I hope this helps,

Cor

To add a question regarding what you are doing, and in conjunction with the
good answer that Cor has posted:

You want to check in the base class whether or not the current instance is a
base class or derived from the base class? If this is true, I can't think
of a reason why you would ever want to check this inside of a base class.
Maybe you do have a good reason...but, since I don't know a solution
(without hardcoding each and every derived class' name and passing to
GetType()), what is the layout/setup of your code. I would like to know to
see what the real problem is to see if we can come to a better solution to
what you are doing...

If you really want to tell, here is a sample of your problem, and a way to
do it:

Public Class InternalMain
<STAThread()> _
Public Shared Sub Main()
Dim a As MyBaseClass = New MyBaseClass()
Dim b As MyDerivedClass = New MyDerivedClass()

Console.WriteLine("a.IsBaseClass(): " & a.IsBaseClass())
Console.WriteLine("b.IsBaseClass(): " & b.IsBaseClass())
End Sub
End Class

Public Class MyBaseClass
Public ReadOnly Property IsBaseClass() As Boolean
Get
' Return whether or not the current object is an instance
' of this class or a derived class.
Return Me.GetType() Is GetType(MyBaseClass)
End Get
End Property
End Class

Public Class MyDerivedClass
Inherits MyBaseClass
End Class

' The above code, when compiled as a console program, displays:

a.IsBaseClass(): True
b.IsBaseClass(): False


HTH :)

Mythran
 
I'd second Roger's comment.

Although the TypeOf code will work, a system where you have lots of
'Do this if the object of type X' select statements is a nightmare to
extend and maintain.

In many cases, the code can be redesigned to work with overrideable
functions and polymorphism. It is more complicated, and may require an
amount of redesign but, long run, it can provide a more robust and
extensible system.
 
I have a class (employee). This class can be used alone but is also a
base class for more specific types of employees that require further
data. .. . .
Is there a way for the "employee" class instance to look at itself
and tell whether or not is part of a derived "attorney" instance or
is a stand-alone instance?

Why would it care?
If you take an "object" (be it employee /or/ attorney) and try to
do "things" with it, it should act in the manner that is correct for the
kind of object it is. Not wishing to "have a go" at attorneys, here's
a simple example :

Const FixedRate As Single = 11.80 ' or whatever ;-)

Class Employee
Overridable Function QuoteFee( ByVal Hours as Single ) as Double
QuoteFee = Hours * FixedRate
End Function
End Class

Class Attorney
Overrides Function QuoteFee( ByVal Hours as Single ) as Double
QuoteFee = MyBase.QuoteFee * 2.5
End Function
End Class

Dim person As New employee
?person.QuoteFee( 3 ) ' gives you one value (35.40)

Dim person As New attorney
?person.QuoteFee( 3 ) ' gives you /another/ value (88.50)

If your employee class really does need to know /anything/ about
the [myriad] class[es] that might be derived from it, there's
something wrong with your class structure (you might be able to
work around it using Interfaces, though)

HTH,
Phill W.
 

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

Back
Top