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

checking object is of type parent class

Hi all,

I have classes: Unit, Transporter, Bus and Car

Transporter inherits from Unit and Bus and Car inherit from
Transporter.

I want to be able to do
Bus bus = new Bus()
if (bus is Transporter)
{
//....
}

Apparently .net only validates bus is Bus .

How can i do this?

Thanks
Stijn
Oct 30 '08 #1
7 2935
Hi Tarscher,

Can you check that your class declarations are correct, or post them here?
The following sample will yield "true" for isTransporter.

class Unit { }
class Transporter : Unit { }
class Bus : Transporter { }
class Car : Transporter { }

static void Main(string[] args)
{
Bus bus = new Bus();
bool isTransporter = bus is Transporter;
}

--
Stanimir Stoyanov
http://stoyanoff.info

"Tarscher" <ta******@gmail.comwrote in message
news:62**********************************@v28g2000 hsv.googlegroups.com...
Hi all,

I have classes: Unit, Transporter, Bus and Car

Transporter inherits from Unit and Bus and Car inherit from
Transporter.

I want to be able to do
Bus bus = new Bus()
if (bus is Transporter)
{
//....
}

Apparently .net only validates bus is Bus .

How can i do this?

Thanks
Stijn
Oct 30 '08 #2
On 10/30/2008 11:53 AM, Tarscher wrote:
Hi all,

I have classes: Unit, Transporter, Bus and Car

Transporter inherits from Unit and Bus and Car inherit from
Transporter.

I want to be able to do
Bus bus = new Bus()
if (bus is Transporter)
{
//....
}

Apparently .net only validates bus is Bus .

How can i do this?
What you have should cause the code to go inside the if block. (bus is
Trasporter) evaluates to true in my quick test.

--
Tom Porterfield
Oct 30 '08 #3
On Thu, 30 Oct 2008 08:53:10 -0700, Tarscher <ta******@gmail.comwrote:
Hi all,

I have classes: Unit, Transporter, Bus and Car

Transporter inherits from Unit and Bus and Car inherit from
Transporter.

I want to be able to do
Bus bus = new Bus()
if (bus is Transporter)
{
//....
}

Apparently .net only validates bus is Bus .

How can i do this?
As others have pointed out, the code you posted, assuming declarations are
actually as you say they are, should result in the condition in the if()
statement evaluating as true.

But, that said...the code seems suspect to me. The relationship between
Bus and Transporter is known at compile time. The expression has no way
to _not_ evaluate as true, assuming Bus really does inherit Transporter.
So, if you're writing code that checks that condition, there's a good
chance you're going about something the wrong way anyway.

Without seeing a full code example that completely describes your design
and intent here, it's hard to offer anything more specific than that. But
on the face of it, your code seems flawed, even if it should do what you
seem to expect it to do.

Pete
Oct 30 '08 #4
Apparently .net only validates bus is Bus .
And that is the correct answer. Bus is only a Bus :)
But you can handle a BUS "as" a Unit
so the correct way is
if ( (bus as Transporter) !=null
Oct 30 '08 #5
Correct me if I am wrong, but isn't the 'as' clause a short-hand operation
of

if (bus is Transporter)
return (Transporter) bus;
else
return null;

in which case the 'is' test would return true?

I believe the problem lies in Stijn's declaration of the classes.

--
Stanimir Stoyanov
http://stoyanoff.info

"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:21**********************************@34g2000h sh.googlegroups.com...
>
>Apparently .net only validates bus is Bus .

And that is the correct answer. Bus is only a Bus :)
But you can handle a BUS "as" a Unit
so the correct way is
if ( (bus as Transporter) !=null
Oct 30 '08 #6
On 30 okt, 19:09, "Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote:
On Thu, 30 Oct 2008 08:53:10 -0700,Tarscher<tarsc...@gmail.comwrote:
Hi all,
I have classes: Unit, Transporter, Bus and Car
Transporter inherits from Unit and Bus and Car inherit from
Transporter.
I want to be able to do
Bus bus = new Bus()
if (bus is Transporter)
{
//....
}
Apparently .net only validates bus is Bus .
How can i do this?

As others have pointed out, the code you posted, assuming declarations are *
actually as you say they are, should result in the condition in the if() *
statement evaluating as true.

But, that said...the code seems suspect to me. *The relationship between *
Bus and Transporter is known at compile time. *The expression has no way *
to _not_ evaluate as true, assuming Bus really does inherit Transporter. *
So, if you're writing code that checks that condition, there's a good *
chance you're going about something the wrong way anyway.

Without seeing a full code example that completely describes your design *
and intent here, it's hard to offer anything more specific than that. *But *
on the face of it, your code seems flawed, even if it should do what you *
seem to expect it to do.

Pete
Thanks all for the replies.

Due to a bug in my code true was not returned (I checked something
else in bus against Transporter). The example provided is jus t a
simplification of my code.

Regards,
Stijn

Oct 31 '08 #7
Ignacio Machin ( .NET/ C# MVP ) wrote:
>Apparently .net only validates bus is Bus .

And that is the correct answer. Bus is only a Bus :)
But you can handle a BUS "as" a Unit
so the correct way is
if ( (bus as Transporter) !=null
I have to disagree. A bus is also a Transporter (and a Unit based on
the original post). A quick test will show this to be true, and is the
documented behavior for "is".

An is expression evaluates to true if the provided expression is
non-null, and the provided object can be cast to the provided type
without causing an exception to be thrown.
--
Tom Porterfield
Oct 31 '08 #8

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

Similar topics

5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
14
by: Frances Del Rio | last post by:
if (parent.frames.main.location == 'mediaselect.html') { I have a very simple frameset, name of frame where I'm checking is 'main'... why is this not working? I mean this is correct syntax,...
10
by: Macka | last post by:
A few pieces of information first: * I have a class called Folder which represents a row of data in a database table. The data access side of things is not an issue. * The table has a parent...
5
by: Dave | last post by:
How do I check in a Windows Forms app if any controls have changed? I have a form that collects data, and I want to prompt the user if they try to exit the app, or load a new file, without saving...
5
by: Stephen Travis | last post by:
I'm trying to ReDim a child object after passing the parent object as System.Object and it throws a System.InvalidCastException: Cast from type 'Object()' to type 'ChildType()' is not valid. If I...
5
by: RSH | last post by:
I am using code to create a new modal form from a parent form. It is not an MDI form. I need to know if an instance of the form already exists so when the user clicks on the button I can close...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
4
by: Flomo Togba Kwele | last post by:
I am having trouble with the following design. I want an object containing a Parent object and a List of its Child objects. The Child object is abstract, so the List must contain concrete objects...
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.