Thread: collections

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    collections

    can anyone help me with follwing issue:


    StoredList s1 = new SortedList();
    StoredList s2 = new SortedList();

    s1.Add(1, "a");
    s1.Add(2, "b");
    s2.Add(1, "a");
    s2.Add(2, "b");

    s1.Equals(s2);

    how can i use properly s1.Equals(s2);
    even if i properly orverride Equals it always return false.
    if i put code into some other method and test for euality it behave properly.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    >> if i put code into some other method and test for euality it behave properly.

    Can you show us that part?

    >> even if i properly orverride Equals...

    and that part too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    4
    here it is:

    Code:
    using System;
    using System.Collections;
    
    public class Test 
    {
    	public static SortedList s1 = new SortedList();
    	public static SortedList s2 = new SortedList();
    	static void Maker() 
    	{
    		s1.Add(1, "daniel");
    		s1.Add(2, "aceman");
    		s2.Add(1, "daniel");
    		s2.Add(2, "aceman");
    	}
    	static void Main() 
    	{
    		Maker();
    		bool result = Testi(s1, s2);
    		bool result1 = Equals(s1, s2);
    		Console.WriteLine(result);
    		Console.WriteLine(result1);
    	}
    	public static override bool Equals(object obj1, object obj2) {
    		SortedList s1 = obj1 as SortedList;
    		SortedList s2 = obj2 as SortedList;
    		int j = 0;
    		bool a = false;
    		if(s1.Count == s2.Count) 
    		{
    			a = true;
    			for(int i = 0; i < s1.Count; i++) 
    			{
    				if(s1.GetKey(i).Equals(s2.GetKey(i))) j++;
    			}
    			for(int i = 0; i < s1.Count; i++)
    			{
    				if(s1.GetByIndex(i).Equals(s2.GetByIndex(i))) j++;
    			}												  
    		}
    		if((a == true) && (j == (s1.Count)*2)) return true;
    		else return true;
    
    	}
    	static bool Testi(SortedList s1, SortedList s2) 
    	{
    		int j = 0;
    		bool a = false;
    		if(s1.Count == s2.Count) 
    		{
    			a = true;
    			for(int i = 0; i < s1.Count; i++) 
    			{
    				if(s1.GetKey(i).Equals(s2.GetKey(i))) j++;
    			}
    			for(int i = 0; i < s1.Count; i++)
    			{
    				if(s1.GetByIndex(i).Equals(s2.GetByIndex(i))) j++;
    			}												  
    		}
    		if((a == true) && (j == (s1.Count)*2)) return true;
    		else return true; 
    	}
    }
    overriding Equals(object obj) and appropriate code also return false, even if i only put into it onlt {return true;}
    :?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Strange. Given this line:
    else return true;
    I would say it always returns true...

    Yes, actually these functions simply CAN'T return false. There must be an error in the code you posted.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    4
    something like:

    equals
    public boolean equals(Object o)Compares the specified object with this map for equality. Returns true if the given object is also a map and the two Maps represent the same mappings. More formally, two maps t1 and t2 represent the same mappings if t1.entrySet().equals(t2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface.

    Overrides:
    equals in class Object
    Parameters:
    o - object to be compared for equality with this map.
    Returns:
    true if the specified object is equal to this map.

    http://java.sun.com/j2se/1.4.1/docs/.../util/Map.html

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    4
    sorry, there was an mistake in code:

    Code:
    static bool Testi(SortedList s1, SortedList s2) 
    	{
    		int j = 0;
    		bool a = false;
    		if(s1.Count == s2.Count) 
    		{
    			a = true;
    			for(int i = 0; i < s1.Count; i++) 
    			{
    				if(s1.GetKey(i).Equals(s2.GetKey(i))) j++;
    			}
    			for(int i = 0; i < s1.Count; i++)
    			{
    				if(s1.GetByIndex(i).Equals(s2.GetByIndex(i))) j++;
    			}												  
    		}
    		if((a == true) && (j == (s1.Count)*2)) return true;
    		else return true; 
    	}
    should be:
    Code:
    static bool Testi(SortedList s1, SortedList s2) 
    	{
    		int j = 0;
    		bool a = false;
    		if(s1.Count == s2.Count) 
    		{
    			a = true;
    			for(int i = 0; i < s1.Count; i++) 
    			{
    				if(s1.GetKey(i).Equals(s2.GetKey(i))) j++;
    			}
    			for(int i = 0; i < s1.Count; i++)
    			{
    				if(s1.GetByIndex(i).Equals(s2.GetByIndex(i))) j++;
    			}												  
    		}
    		if((a == true) && (j == (s1.Count)*2)) return true;
    		else return false; 
    	}
    i am sure that i wrote inproperly code in overriden function equals but i am not sure in what way.
    function testi behave in a way i want it. but function equals does not.
    i'll try to put soted list into two separated objects (references to class) i should do.

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You overwrote and used the function Test.Equals. In your case, I would derive a class from SortedList and override this classes Equals method. Put the code you have checked inside this class and it should function properly.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 01:06 PM
  2. Messing with listbox collections and random fun
    By DanFraser in forum C# Programming
    Replies: 0
    Last Post: 06-20-2005, 07:40 PM
  3. foreach with collections
    By Korn1699 in forum C# Programming
    Replies: 1
    Last Post: 12-03-2004, 03:12 AM
  4. Icon collections
    By _Elixia_ in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-12-2003, 04:41 PM
  5. Collections
    By Klaas in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2002, 03:53 PM