checking object is of type parent class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tarscher

    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
  • Stanimir Stoyanov

    #2
    Re: checking object is of type parent class

    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
    메이저사이트 순위 먹튀검증이 완료된 토토사이트 커뮤니티 - 토토핫 입니다. 저희 토토핫에서는 베팅할때 필수로 점검해야 되는 안전 가이드를 제공하며 안전한 메이저 놀이터 목록과 메이저사이트 먹튀검증 꽁머니사이트를 추천드립니다.


    "Tarscher" <tarscher@gmail .comwrote in message
    news:62493dcf-4d66-420d-80ff-4e542383602b@v2 8g2000hsv.googl egroups.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

    Comment

    • Tom Porterfield

      #3
      Re: checking object is of type parent class

      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

      Comment

      • Peter Duniho

        #4
        Re: checking object is of type parent class

        On Thu, 30 Oct 2008 08:53:10 -0700, Tarscher <tarscher@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

        Comment

        • Ignacio Machin ( .NET/ C# MVP )

          #5
          Re: checking object is of type parent class

          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

          Comment

          • Stanimir Stoyanov

            #6
            Re: checking object is of type parent class

            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
            메이저사이트 순위 먹튀검증이 완료된 토토사이트 커뮤니티 - 토토핫 입니다. 저희 토토핫에서는 베팅할때 필수로 점검해야 되는 안전 가이드를 제공하며 안전한 메이저 놀이터 목록과 메이저사이트 먹튀검증 꽁머니사이트를 추천드립니다.


            "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
            message
            news:21dad7af-85bd-4037-bb36-35aca79e775c@34 g2000hsh.google groups.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

            Comment

            • Tarscher

              #7
              Re: checking object is of type parent class

              On 30 okt, 19:09, "Peter Duniho" <NpOeStPe...@nn owslpianmk.comw rote:
              On Thu, 30 Oct 2008 08:53:10 -0700,Tarscher<t arsc...@gmail.c omwrote:
              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



              Comment

              • Tom Porterfield

                #8
                Re: checking object is of type parent class

                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

                Comment

                Working...