473,387 Members | 1,502 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,387 software developers and data experts.

Am I part of a derived class?

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

Nov 21 '05 #1
6 985
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/de...ttypetopic.asp
I hope this helps,

Cor
Nov 21 '05 #2


gw*******@millermartin.com wrote:
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

--
Larry Lard
Replies to group please

Nov 21 '05 #3

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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/de...ttypetopic.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

Nov 21 '05 #4
Hi,
Have you looked into polymorphism (virtual methods)?
Roger
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

Nov 21 '05 #5
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.

Nov 21 '05 #6
<gw*******@millermartin.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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.

Nov 21 '05 #7

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

Similar topics

0
by: Anand | last post by:
class base: def __setattr__(self,attr,key,*unexpected): print "Base Class :",attr,key,unexpected,self.__dict__ self.__dict__ = key def __getattr__(self,attr,*unexpected): print "Base Class...
10
by: Alf P. Steinbach | last post by:
The fifth part of my attempted Correct C++ tutorial is now available, although for now only in Word format (use free Open Office if no Word), and also, it's not yet been reviewed at all -- ...
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
2
by: Gill Bates | last post by:
Hope I'm not being a Pain In the A.. However; now I've got a similar issue for which I've seen no work around... Module Module1 Public Class BaseRow Public Overridable Function Speak() As...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.