Compare SortedList problem.
-
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; }
-
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; }
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
-
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
That just turns things around. Now everything is different.