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