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. Comparing objects of different types

Comparing objects of different types

Scheduled Pinned Locked Moved C#
comdata-structuresquestionlearning
10 Posts 2 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.
  • W Offline
    W Offline
    Waleed Eissa
    wrote on last edited by
    #1

    In my application I have an array of custom objects that I want to search by one of the properties of the custom object (the ID property which is an int), please see the code below to understand what I'm trying to do:

    private CustomObject[] _customObjects;
    public CustomObject CustomObjects
    {
    get
    {
    if(_customObjects == null)
    {
    CustomObject[] customObjects = CustomObject.GetCustomObjects();
    Array.Sort(customObjects); // CustomObject implements IComparable
    _customObjects = customObjects;
    }
    }

    public IsIDInList(int id)
    {
    // What do I have to do to make this work?
    return Array.BinarySearch(CustomObjects, id) >= 0;
    }

    I could use a dictionary of course but I don't want to for some reasons specific to my application. Any suggestions are highly appreciated...

    Waleed Eissa Software Developer Sydney

    L 1 Reply Last reply
    0
    • W Waleed Eissa

      In my application I have an array of custom objects that I want to search by one of the properties of the custom object (the ID property which is an int), please see the code below to understand what I'm trying to do:

      private CustomObject[] _customObjects;
      public CustomObject CustomObjects
      {
      get
      {
      if(_customObjects == null)
      {
      CustomObject[] customObjects = CustomObject.GetCustomObjects();
      Array.Sort(customObjects); // CustomObject implements IComparable
      _customObjects = customObjects;
      }
      }

      public IsIDInList(int id)
      {
      // What do I have to do to make this work?
      return Array.BinarySearch(CustomObjects, id) >= 0;
      }

      I could use a dictionary of course but I don't want to for some reasons specific to my application. Any suggestions are highly appreciated...

      Waleed Eissa Software Developer Sydney

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I suggest you have a look at this overload: Array.BinarySearch Method (Array, Int32, Int32, Object, IComparer) and if necessary read up on IComparer. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Voting for dummies? No thanks. X|


      W 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, I suggest you have a look at this overload: Array.BinarySearch Method (Array, Int32, Int32, Object, IComparer) and if necessary read up on IComparer. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Voting for dummies? No thanks. X|


        W Offline
        W Offline
        Waleed Eissa
        wrote on last edited by
        #3

        Hi, thanks for your reply, actually I know about IComparer and IComparable and I use them very often in my code but I never had the need to compare objects of different types, this is why I'm a little confused, I know it's not possible using the generic version of IComparer, are you referring to the non-generic version? A short code sample would very helpful, thanks a lot.

        Waleed Eissa Software Developer Sydney

        L 1 Reply Last reply
        0
        • W Waleed Eissa

          Hi, thanks for your reply, actually I know about IComparer and IComparable and I use them very often in my code but I never had the need to compare objects of different types, this is why I'm a little confused, I know it's not possible using the generic version of IComparer, are you referring to the non-generic version? A short code sample would very helpful, thanks a lot.

          Waleed Eissa Software Developer Sydney

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          :confused: All your objects are of type CustomObject, aren't they? (either directly or by inheritance) If you can Sort the Array, why wouldn't you be able to BinarySearch it? :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Voting for dummies? No thanks. X|


          W 1 Reply Last reply
          0
          • L Luc Pattyn

            :confused: All your objects are of type CustomObject, aren't they? (either directly or by inheritance) If you can Sort the Array, why wouldn't you be able to BinarySearch it? :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Voting for dummies? No thanks. X|


            W Offline
            W Offline
            Waleed Eissa
            wrote on last edited by
            #5

            Hmm, seems you didn't read my post very carefully :), I have an array of CustomObject objects, CustomObject has a property named ID which is an int, I want to search the array for a custom object with that ID or in other words I want to be able to know whether there's a CustomObject in the array with that ID, I'm not interested in returning the object, I just want to know whether it's in the array or not. Regards

            Waleed Eissa Software Developer Sydney

            L 1 Reply Last reply
            0
            • W Waleed Eissa

              Hmm, seems you didn't read my post very carefully :), I have an array of CustomObject objects, CustomObject has a property named ID which is an int, I want to search the array for a custom object with that ID or in other words I want to be able to know whether there's a CustomObject in the array with that ID, I'm not interested in returning the object, I just want to know whether it's in the array or not. Regards

              Waleed Eissa Software Developer Sydney

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              :confused::confused: Array.BinarySearch will look for an element with a specific Property, provided your IComparer is based on that same Property, hence the Array must be Sorted accordingly. And it will return a positive index when found, or a negative number when not found. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Voting for dummies? No thanks. X|


              W 1 Reply Last reply
              0
              • L Luc Pattyn

                :confused::confused: Array.BinarySearch will look for an element with a specific Property, provided your IComparer is based on that same Property, hence the Array must be Sorted accordingly. And it will return a positive index when found, or a negative number when not found. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Voting for dummies? No thanks. X|


                W Offline
                W Offline
                Waleed Eissa
                wrote on last edited by
                #7

                I only have the value of the ID property (have a look on the code), I don't have the object, can I pass objects of different types to IComparer.Compare()?

                Waleed Eissa Software Developer Sydney

                L 1 Reply Last reply
                0
                • W Waleed Eissa

                  I only have the value of the ID property (have a look on the code), I don't have the object, can I pass objects of different types to IComparer.Compare()?

                  Waleed Eissa Software Developer Sydney

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Hi, if your IComparer only looks at the object's ID, you could pass it a dummy object with the ID value you are looking for. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Voting for dummies? No thanks. X|


                  W 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, if your IComparer only looks at the object's ID, you could pass it a dummy object with the ID value you are looking for. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    Voting for dummies? No thanks. X|


                    W Offline
                    W Offline
                    Waleed Eissa
                    wrote on last edited by
                    #9

                    Hmm, so this means to create an object and assign it the value I want for the ID just for the sake of passing to the IComparer, is my understanding correct? So, as far as I can see, comparing objects of different types is not directly supported by the framework, is this correct?

                    Waleed Eissa Software Developer Sydney

                    L 1 Reply Last reply
                    0
                    • W Waleed Eissa

                      Hmm, so this means to create an object and assign it the value I want for the ID just for the sake of passing to the IComparer, is my understanding correct? So, as far as I can see, comparing objects of different types is not directly supported by the framework, is this correct?

                      Waleed Eissa Software Developer Sydney

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      Waleed Eissa wrote:

                      to create an object and assign it the value I want for the ID

                      that was the idea yes

                      Waleed Eissa wrote:

                      comparing objects of different types is not directly supported

                      objects of different types are different by definition, no need to compare them. this is not related to any framework, they differ by definition in OO. chairs aren't tables. If your different types have a useful common ancestor (say furniture) you should use that as the basis for your logic. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      Voting for dummies? No thanks. X|


                      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