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. Compare SortedList problem.

Compare SortedList problem.

Scheduled Pinned Locked Moved C#
helpquestion
3 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.
  • D Offline
    D Offline
    dennycrane
    wrote on last edited by
    #1

    This code almost works. There is a problem with the compare constructor. Something is wrong there because it shows everything as correct. Any ideas?

    public void ChkSp()
            {
    
                SortedList SL = new SortedList();
                SortedList SL2 = new SortedList();
                
                SL.Add(0, "Value1");
                SL.Add(1, "Value2");
                SL.Add(2, "Value3");
                SL.Add(3, "Value4");
    
                SL2.Add(0, richTextBox1.Text);
                SL2.Add(1, richTextBox1.Text);
                SL2.Add(2, richTextBox1.Text);
                SL2.Add(3, richTextBox1.Text);
                
                bool equal = Compare(SL, SL2);
    
                if (equal)
                {
                    richTextBox2.Text = "Correct!";
                }
                else
                { 
                    richTextBox2.Text = "They differ";
                    
                }
            }
            
          
            static bool Compare(SortedList SL, SortedList SL2)
            {
    
                      if (SL.Count != SL2.Count)
                      {
                          return false;
                      }
                      
                   
                     foreach (DictionaryEntry item in SL)
                     {
                         if (!SL2.ContainsKey(item.Key))
                         {
                             
                             return false;
                         }
    
                       
                     }
                
                    
                     return true;
                
                    
            }
    
    G 1 Reply Last reply
    0
    • D dennycrane

      This code almost works. There is a problem with the compare constructor. Something is wrong there because it shows everything as correct. Any ideas?

      public void ChkSp()
              {
      
                  SortedList SL = new SortedList();
                  SortedList SL2 = new SortedList();
                  
                  SL.Add(0, "Value1");
                  SL.Add(1, "Value2");
                  SL.Add(2, "Value3");
                  SL.Add(3, "Value4");
      
                  SL2.Add(0, richTextBox1.Text);
                  SL2.Add(1, richTextBox1.Text);
                  SL2.Add(2, richTextBox1.Text);
                  SL2.Add(3, richTextBox1.Text);
                  
                  bool equal = Compare(SL, SL2);
      
                  if (equal)
                  {
                      richTextBox2.Text = "Correct!";
                  }
                  else
                  { 
                      richTextBox2.Text = "They differ";
                      
                  }
              }
              
            
              static bool Compare(SortedList SL, SortedList SL2)
              {
      
                        if (SL.Count != SL2.Count)
                        {
                            return false;
                        }
                        
                     
                       foreach (DictionaryEntry item in SL)
                       {
                           if (!SL2.ContainsKey(item.Key))
                           {
                               
                               return false;
                           }
      
                         
                       }
                  
                      
                       return true;
                  
                      
              }
      
      G Offline
      G Offline
      Gopal S
      wrote on last edited by
      #2

      Hi Dennycrane You are checking a 'Key value(0 to 3)' only, not a value (Value1). Both collection contain same keys(0 to 4). So always return true. You can check the values using "ContainsValue" method instead of 'ContainsKey'. Ex: if (!SL2.ContainsValue(item.Value)) { return false; } Thanks,

      Gopal.S

      D 1 Reply Last reply
      0
      • G Gopal S

        Hi Dennycrane You are checking a 'Key value(0 to 3)' only, not a value (Value1). Both collection contain same keys(0 to 4). So always return true. You can check the values using "ContainsValue" method instead of 'ContainsKey'. Ex: if (!SL2.ContainsValue(item.Value)) { return false; } Thanks,

        Gopal.S

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

        That just turns things around. Now everything is different.

        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