PC Review


Reply
Thread Tools Rate Thread

checking object is of type parent class

 
 
Tarscher
Guest
Posts: n/a
 
      30th Oct 2008
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
 
Reply With Quote
 
 
 
 
Stanimir Stoyanov
Guest
Posts: n/a
 
      30th Oct 2008
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" <(E-Mail Removed)> wrote in message
news:62493dcf-4d66-420d-80ff-(E-Mail Removed)...
> 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


 
Reply With Quote
 
Tom Porterfield
Guest
Posts: n/a
 
      30th Oct 2008
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
 
Reply With Quote
 
Ignacio Machin ( .NET/ C# MVP )
Guest
Posts: n/a
 
      30th Oct 2008

> 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
 
Reply With Quote
 
Stanimir Stoyanov
Guest
Posts: n/a
 
      30th Oct 2008
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 )" <(E-Mail Removed)> wrote in
message
news:21dad7af-85bd-4037-bb36-(E-Mail Removed)...
>
>> 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


 
Reply With Quote
 
Tarscher
Guest
Posts: n/a
 
      31st Oct 2008
On 30 okt, 19:09, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> wrote:
> On Thu, 30 Oct 2008 08:53:10 -0700,Tarscher<tarsc...@gmail.com> 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?

>
> 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



 
Reply With Quote
 
Tom Porterfield
Guest
Posts: n/a
 
      31st Oct 2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting a C# base class object to a derived class type. =?Utf-8?B?Q2hyaXMgQ2Fwb24=?= Microsoft C# .NET 5 3rd Jun 2005 11:35 PM
Telling what type an object is and checking type Brian Henry Microsoft VB .NET 3 13th Apr 2004 03:22 PM
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 leal ting Microsoft ASP .NET 0 29th Dec 2003 07:08 AM
How to get parent class type? Robin.Liu Microsoft C# .NET 2 17th Dec 2003 09:10 AM
Determine the object (parent?) from an inherited class. dsandor Microsoft C# .NET 2 20th Aug 2003 02:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:52 AM.