473,654 Members | 3,190 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 7429
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************* *@exisconsultin g.com

"dsandor" <dsandor@_NOSPA M_emailanywhere .net> wrote in message
news:%2******** ********@TK2MSF TNGP12.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@_NOSPA M_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.co m>
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
2398
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 not even relative with implementation - I'm looking for a smart solution My present system works like this:
0
1264
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 my data structure : A project would be made of tasks, and each task can contain sub-tasks, so I would have something like
5
6495
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 indicated by the member attibute class_BASE *mElement;
3
3941
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 form in button2 clicked event: textbox1.Text = "Hello";
2
6176
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 form is MDIChild this codes show MDIParents name. How can I show parent form name (not MDIParent) public class MyButton: System.Windows.Forms.Button{ { ....
6
3008
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
1
1326
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 from ArrayList public class Fields: ArrayList
3
5855
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 type of the class inherited from, the base class.
3
1708
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 explanation of what I have and in which direction I'm thinking: CLASS DESIGN ============= 1. Generic class "Company" contains general company information (name, address, reference person, phone, etc...)
0
8379
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.