473,503 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine the object (parent?) from an inherited class.

I have a class that is inherited by another class. Let say:

public class vehicle
{
public string color
{
get ...
set ...
}
}

and..

public class truck : vehicle
{
property int wheels
{
get...
set...
}
}

From within the vehicle class I would like to be able to look at properties
in the truck class. Can this be done with reflection? How can I know what
type of object the 'inheriter' is?

Thanks,
David Sandor
Nov 15 '05 #1
2 7417
David,

If the properties are public or protected, then you have access to them
already. If some of the methods or properties are virtual, and you want to
access the base class functionality, you can use the "base" keyword (much
like "this") and make the calls to those methods/properties.

If you want to access private variables, then you can easily use
reflection on the current type to get the private method/field/property
information, but generally this is a bad idea, as the internals of the class
are not guaranteed to not change.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"dsandor" <dsandor@_NOSPAM_emailanywhere.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I have a class that is inherited by another class. Let say:

public class vehicle
{
public string color
{
get ...
set ...
}
}

and..

public class truck : vehicle
{
property int wheels
{
get...
set...
}
}

From within the vehicle class I would like to be able to look at properties in the truck class. Can this be done with reflection? How can I know what type of object the 'inheriter' is?

Thanks,
David Sandor

Nov 15 '05 #2
dsandor <dsandor@_NOSPAM_emailanywhere.net> wrote:
I have a class that is inherited by another class. Let say:

public class vehicle
{
public string color
{
get ...
set ...
}
}

and..

public class truck : vehicle
{
property int wheels
{
get...
set...
}
}

From within the vehicle class I would like to be able to look at properties
in the truck class.
You can do that by casting:

Truck t = (Truck) this;
int w = t.Wheels;

etc

It's normally not a good idea though - there are usually better ways of
doing things using polymorphism etc.
How can I know what type of object the 'inheriter' is?


if (this is Truck)
{
....
}

etc - or use GetType()

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3

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

Similar topics

12
2375
by: R | last post by:
Hello everybody. I'm writing my own Content System in PHP5. I've written so far main classes for handling DB connections, XML, XForms and Sessions. But I've got problem with one thing - it's...
0
1255
by: Olivier Jullian | last post by:
Hi, I'm new to .NET and am trying to take advantage of the object structure while accessing relational databases. I started a small project for managing "projects". Here is a description of...
5
6482
by: Christian Christmann | last post by:
Hi, I've a some classes which are all inherited from the same base class class_BASE; There's another class which holds some information and is "bound" to one of the inherited classes...
3
3932
by: Omar Llanos | last post by:
I have Form1 and Form2 (which is inherited from Form1), and I created a button in Form2 that will fill up a textbox in Form1. What code would do that? I tried the simplest way: //from child...
2
6165
by: Levis | last post by:
Hi, I inherited Button and I created my component (MyButton) In this button onclick event I want show this objects parent form. When my form is not MDIChild this codes run correct. But when my...
6
3004
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
1
1321
by: leal ting | last post by:
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList lealting@hotmail.com] the class inherited...
3
5851
by: Magnus | last post by:
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...
3
1698
by: Froefel | last post by:
I'm trying to modem a relationship with classes and I'm having trouble finding the correct design pattern. Maybe someone with more experience knows which pattern(s) I'm looking for. Here's an...
0
7202
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
7086
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
7330
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...
1
6991
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...
0
7460
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...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
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...
0
3167
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...
0
1512
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 ...

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.