checking object is of type parent class

  • Thread starter Thread starter Tarscher
  • Start date Start date
T

Tarscher

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
 
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;
}
 
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.
 
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
 
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.
 
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
 
Ignacio said:
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.
 
Back
Top