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. SortedList - KeyValuePair - InvalidCastException

SortedList - KeyValuePair - InvalidCastException

Scheduled Pinned Locked Moved C#
19 Posts 3 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.
  • D dennycrane

    Then what should I do?

    C Offline
    C Offline
    carbon_golem
    wrote on last edited by
    #10

    But it's a list. That'll bugger things up. A.items == B.items & B.items = A.items Duplicates could mess you up. pseudocode: foreach thingy in A{ make sure thingy is in B.Thingies } foreach thingy in B { make sure thingy is in A.Thingies } Help any?

    "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

    D 1 Reply Last reply
    0
    • D dennycrane

      Which I have. This is the Compare code. I suspect it has something to do with the bold text. BUt I can´t put my finger on it.

        static bool Compare(SortedList SL, SortedList SL2)
              {
                  // Early check for difference
                  if (SL.Count != SL2.Count)
                  {
                      return false;
                  }
                  
                  // Compare each value in list 1 with each value in list 2
                  foreach (DictionaryEntry item in SL)
                  {
                      if (!SL.ContainsKey(item.Key))
                      {
                          // Return the moment we find a difference
                          return false;
                      }
                      
                  }
                  
                  // Must be the same
                  return true;
                  
              }
      
      C Offline
      C Offline
      carbon_golem
      wrote on last edited by
      #11

      Is this homework?

      "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

      D 1 Reply Last reply
      0
      • C carbon_golem

        But it's a list. That'll bugger things up. A.items == B.items & B.items = A.items Duplicates could mess you up. pseudocode: foreach thingy in A{ make sure thingy is in B.Thingies } foreach thingy in B { make sure thingy is in A.Thingies } Help any?

        "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

        D Offline
        D Offline
        dennycrane
        wrote on last edited by
        #12

        not really, if you could incorporate it into the Compare code I posted it may shed some light.

        C 1 Reply Last reply
        0
        • C carbon_golem

          Is this homework?

          "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

          D Offline
          D Offline
          dennycrane
          wrote on last edited by
          #13

          Not really, I am taking a course in C# but this is more of a side project I´d like to finish.

          1 Reply Last reply
          0
          • D dennycrane

            Which I have. This is the Compare code. I suspect it has something to do with the bold text. BUt I can´t put my finger on it.

              static bool Compare(SortedList SL, SortedList SL2)
                    {
                        // Early check for difference
                        if (SL.Count != SL2.Count)
                        {
                            return false;
                        }
                        
                        // Compare each value in list 1 with each value in list 2
                        foreach (DictionaryEntry item in SL)
                        {
                            if (!SL.ContainsKey(item.Key))
                            {
                                // Return the moment we find a difference
                                return false;
                            }
                            
                        }
                        
                        // Must be the same
                        return true;
                        
                    }
            
            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #14

            Here's a hint - you're reading from the SL sorted list, and then checking to see if the same sorted list contains the key. Change it to point to the right list:

            foreach (DictionaryEntry item in SL)
            {
            if (!SL2.ContainsKey(item.Key))
            {

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            D 1 Reply Last reply
            0
            • D dennycrane

              not really, if you could incorporate it into the Compare code I posted it may shed some light.

              C Offline
              C Offline
              carbon_golem
              wrote on last edited by
              #15

              I'm not going to write code for you, call it tough love. Again, I'll write pseudo code for you, you're supposed to be writing it to learn. using SortedList, in this case you can go through by INDEX and check to see if the elements that life in the INDEXES match. for(0 to elements){ if a[index] != b[index], return false. } return true. Hope this helps.

              "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

              D 1 Reply Last reply
              0
              • C carbon_golem

                I'm not going to write code for you, call it tough love. Again, I'll write pseudo code for you, you're supposed to be writing it to learn. using SortedList, in this case you can go through by INDEX and check to see if the elements that life in the INDEXES match. for(0 to elements){ if a[index] != b[index], return false. } return true. Hope this helps.

                "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

                D Offline
                D Offline
                dennycrane
                wrote on last edited by
                #16

                what do you mean by 0 to elements?

                C 1 Reply Last reply
                0
                • P Pete OHanlon

                  Here's a hint - you're reading from the SL sorted list, and then checking to see if the same sorted list contains the key. Change it to point to the right list:

                  foreach (DictionaryEntry item in SL)
                  {
                  if (!SL2.ContainsKey(item.Key))
                  {

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles

                  D Offline
                  D Offline
                  dennycrane
                  wrote on last edited by
                  #17

                  No, that doesn't work.

                  1 Reply Last reply
                  0
                  • D dennycrane

                    what do you mean by 0 to elements?

                    C Offline
                    C Offline
                    carbon_golem
                    wrote on last edited by
                    #18

                    Alright.. you have two lists of KeyValuePairs. Say they're identical. List1 = [1,A],[2,B],[3,C],[4,D] List2 = [1,A],[2,B],[3,C],[4,D] When you have 2 sorted lists, you can use the index to do the check and not the foreach loop. if(List1.Count != List2.Count) return false; for(int i = 0 ; i < List1.Count ; i++){ if(List1[i] != List2[i]) return false; } return true; And you can see when you write the problem out exactly how it happens. Example 2 Inequal list lengths. List1 = [1,A],[2,B],[3,C],[4,D] List2 = [1,A],[2,B],[3,C],[4,D],[5,E] the comparison of lengths returns false. OK. Example 3 Inequal lists List1 = [1,A],[2,B],[3,C],[4,D] List2 = [1,A],[2,B],[3,C],[5,E] remember that these are sorted, so we're COUNTING on the order. List lengths, ok, continue. List1[0] == List2[0] TRUE List1[1] == List2[1] TRUE List1[2] == List2[2] TRUE List1[3] == List2[3] FALSE Lists inequal, ok. Remember that the contents of the list are important, so you have to check both the key and the value in your KeyValuePair. B.Key == A.Key && B.Value == A.Value Hope this helps some more.

                    "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

                    D 1 Reply Last reply
                    0
                    • C carbon_golem

                      Alright.. you have two lists of KeyValuePairs. Say they're identical. List1 = [1,A],[2,B],[3,C],[4,D] List2 = [1,A],[2,B],[3,C],[4,D] When you have 2 sorted lists, you can use the index to do the check and not the foreach loop. if(List1.Count != List2.Count) return false; for(int i = 0 ; i < List1.Count ; i++){ if(List1[i] != List2[i]) return false; } return true; And you can see when you write the problem out exactly how it happens. Example 2 Inequal list lengths. List1 = [1,A],[2,B],[3,C],[4,D] List2 = [1,A],[2,B],[3,C],[4,D],[5,E] the comparison of lengths returns false. OK. Example 3 Inequal lists List1 = [1,A],[2,B],[3,C],[4,D] List2 = [1,A],[2,B],[3,C],[5,E] remember that these are sorted, so we're COUNTING on the order. List lengths, ok, continue. List1[0] == List2[0] TRUE List1[1] == List2[1] TRUE List1[2] == List2[2] TRUE List1[3] == List2[3] FALSE Lists inequal, ok. Remember that the contents of the list are important, so you have to check both the key and the value in your KeyValuePair. B.Key == A.Key && B.Value == A.Value Hope this helps some more.

                      "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

                      D Offline
                      D Offline
                      dennycrane
                      wrote on last edited by
                      #19
                              static bool Compare(SortedList SL, SortedList SL2)
                              {
                                  if(SL.Count != SL2.Count) return false;
                      
                                      for(int i = 0 ; i < SL.Count ; i++)
                                      {
                                          if(SL[i] != SL2[i]) return false;
                                      }
                      
                                      return true;
                      

                      Is that right?

                      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