Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Polymorphic object comparison

C# Polymorphic object comparison

Scheduled Pinned Locked Moved C#
csharptutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Benny_Lava
    wrote on last edited by
    #1

    Hi, Let's say there are 3 objects: Class A Class B : A Class C : B I want to pass through all objects that derive from Type B C obj1 = new C(); B obj2 = new B(); if(obj1.GetType() == typeof(B)) { //--Pass through all objects that derive from object B or object A... } typeof comapares directly the top types C must equal C... No polymorphic comarison... How to do a polymorphic comparison? Which keyword or mechanism to use? Thanks!

    L OriginalGriffO L 3 Replies Last reply
    0
    • B Benny_Lava

      Hi, Let's say there are 3 objects: Class A Class B : A Class C : B I want to pass through all objects that derive from Type B C obj1 = new C(); B obj2 = new B(); if(obj1.GetType() == typeof(B)) { //--Pass through all objects that derive from object B or object A... } typeof comapares directly the top types C must equal C... No polymorphic comarison... How to do a polymorphic comparison? Which keyword or mechanism to use? Thanks!

      L Offline
      L Offline
      Lukasz Nowakowski
      wrote on last edited by
      #2

      if(obj1 is B) should work for you

      B 1 Reply Last reply
      0
      • B Benny_Lava

        Hi, Let's say there are 3 objects: Class A Class B : A Class C : B I want to pass through all objects that derive from Type B C obj1 = new C(); B obj2 = new B(); if(obj1.GetType() == typeof(B)) { //--Pass through all objects that derive from object B or object A... } typeof comapares directly the top types C must equal C... No polymorphic comarison... How to do a polymorphic comparison? Which keyword or mechanism to use? Thanks!

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        As lukasz_nowakowski says, is is what you want. The alternative is as

        if (obj is B)
        {
        B bInstance = (B) obj;
        ...
        }

        or

        B bInstance = obj as B;
        if (bInstance != null)
        {
        ...
        }

        You can use either, I prefer the second as I prefer explicit null tests. That's just personal preference though.

        Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        L 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          As lukasz_nowakowski says, is is what you want. The alternative is as

          if (obj is B)
          {
          B bInstance = (B) obj;
          ...
          }

          or

          B bInstance = obj as B;
          if (bInstance != null)
          {
          ...
          }

          You can use either, I prefer the second as I prefer explicit null tests. That's just personal preference though.

          Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

          L Offline
          L Offline
          Lukasz Nowakowski
          wrote on last edited by
          #4

          If you want to just add it to objects' list, then is is enough, but if you want to use object obj as instance of class B, then B instance = obj as B; is the path to follow. To find out why read for example this[^].

          B 1 Reply Last reply
          0
          • L Lukasz Nowakowski

            if(obj1 is B) should work for you

            B Offline
            B Offline
            Benny_Lava
            wrote on last edited by
            #5

            Thank you!!

            1 Reply Last reply
            0
            • L Lukasz Nowakowski

              If you want to just add it to objects' list, then is is enough, but if you want to use object obj as instance of class B, then B instance = obj as B; is the path to follow. To find out why read for example this[^].

              B Offline
              B Offline
              Benny_Lava
              wrote on last edited by
              #6

              The work with polymorphic objects after comparison is very clear to me. I just didn't know how to compare them in C#... Thanks!

              1 Reply Last reply
              0
              • B Benny_Lava

                Hi, Let's say there are 3 objects: Class A Class B : A Class C : B I want to pass through all objects that derive from Type B C obj1 = new C(); B obj2 = new B(); if(obj1.GetType() == typeof(B)) { //--Pass through all objects that derive from object B or object A... } typeof comapares directly the top types C must equal C... No polymorphic comarison... How to do a polymorphic comparison? Which keyword or mechanism to use? Thanks!

                L Offline
                L Offline
                LookSharp
                wrote on last edited by
                #7

                FYI - while the use of is and as are what's appropriate for your problem, as lukasz_nowakowski and OriginalGriff have said, your example code used GetType ... which put me in mind of how to do a similar test on Types, rather than objects:

                Type typeOfC = typeof(C);
                Type typeofSomeObject = someObject.GetType();
                
                // ...
                if (typeOfC.IsAssignableFrom(typeofSomeObject))
                	{
                	// ...
                	}
                // ...
                
                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups