473,569 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine type of inherited class

i have a class which inherits from a base class

class base1 {
}

class child1 : base1 {
}

how can I determine if child1 is of base1? gettype returns type of child1,
but I need to know the type of the class inherited from, the base class.
Casting will cause an exception if the base class is of the wrong type, I
would like to avoid any exception approach.

I basically would like to know if I can cast the object to one or the other
type. Any ideas?

Thank you.

- Magnus
Aug 23 '06 #1
3 5853
"Magnus" <no@spam.comwro te in message news:44******** @news.broadpark .no...
how can I determine if child1 is of base1? gettype returns type of child1,
but I need to know the type of the class inherited from, the base class.
Casting will cause an exception if the base class is of the wrong type, I
would like to avoid any exception approach.
if (obj is base1) ...

-- Alan
Aug 23 '06 #2
Magnus,

The 'is' and 'as' operators will do just that. The 'is' operator
evaluates to a bool. The 'as' operator returns a reference of the type
specified or null if the object is not of the specified type.

// Example 1
if (myClass is base1)
{
// whatever
}

// Example 2
base1 myBaseClass = myClass as base1;
if (myBaseClass != null)
{
// whatever
}

Brian

Magnus wrote:
i have a class which inherits from a base class

class base1 {
}

class child1 : base1 {
}

how can I determine if child1 is of base1? gettype returns type of child1,
but I need to know the type of the class inherited from, the base class.
Casting will cause an exception if the base class is of the wrong type, I
would like to avoid any exception approach.

I basically would like to know if I can cast the object to one or the other
type. Any ideas?

Thank you.

- Magnus
Aug 24 '06 #3
Hi,

You can use Type.BaseType:

instance.GetTyp e().BaseType

You could also use "is" or "as" if all you want to do is a test
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Magnus" <no@spam.comwro te in message news:44******** @news.broadpark .no...
>i have a class which inherits from a base class

class base1 {
}

class child1 : base1 {
}

how can I determine if child1 is of base1? gettype returns type of child1,
but I need to know the type of the class inherited from, the base class.
Casting will cause an exception if the base class is of the wrong type, I
would like to avoid any exception approach.

I basically would like to know if I can cast the object to one or the
other type. Any ideas?

Thank you.

- Magnus


Aug 24 '06 #4

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

Similar topics

2
2147
by: Josh Mcfarlane | last post by:
I'm doing recomposition of objects from binary streams, and the best way for me to write them out is to write base class data first, forward to inherited classes, pointer class values, etc. Now, when recomposing these objects, I first read the base class data, and can create a base object. When I find the inherited class marker, can I...
8
3060
by: TS | last post by:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the code to do it? something like this? this.GetType().GetMember("panel1").SetValue(xx).Left = 44;
9
1636
by: Codex Twin | last post by:
Hi I have a common model for a Data Access Layer scenario. I have an abstract base class, called DalBase which contains a list of abstract methods. Lets call them: public abstract void Shine(); public abstract void Flow(); public abstract void Float(); I then have an inherited class, called DalMain which contains the concrete
12
3775
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. 1. The Deserialization goes like: #Region " Load "
10
2563
by: Smokey Grindle | last post by:
i want to inherit the list view class, but in the inherited class, hide the Header style property and the view property (basically its a detailed list with always clickable headers) how do I keep the base class properties from showing up in the inherited class when people change its properties in the IDE? thanks!
5
3600
by: Eliseu Rodrigues | last post by:
Hi I would like to have a static method on a base class that executes some action (for example retrieves the row count) on a table whose name is the same of the inherited class name. For example: Assume that my base class has a static method named GetRowCount().
1
1400
by: R Karthick | last post by:
I am new to serialization. Apologies, if my question sounds really basic. Stuck in this problem for the last two hours. Thought, will ask the experts instead of banging my head more. This is what I have..! <code> Abstract Class FooBase {
12
2149
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a class which "BiologySequence" which looks about like this. public class BiologySequence { private string _Sequence; public string Sequence {
0
950
by: Juri | last post by:
Hi, I have a .NET control hosted in a CWinFormsView. In the CWinFormsView I have enabled events in the .NET control to callback to a method in the CWinFormsView by using the BEGIN_DELEGATE_MAP macro. Everything works just fine, the native method is called properly: void CGraphControlView::ObjectDoubleClicked(System::Object^ sender,...
0
7695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.