Willem
Willem

Reputation: 81

Problems comparing objects using IComparable

Here is some code I'm trying to get working. If an item in one set doesn't match an item in the other set a 0 is added to a list for all items compared. If in the end the list doesn't contain any other values than 0 it means the item from the first set doesn't exist at all in the second set. For some reason or another I keep getting wrong values in the resulting list, so there must be a bug somewhere, it's just that I can't seem to find it.

    public class CompareItem : IComparable
    {
        public string CustId { get; set; }
        public string TechId { get; set; }

        public CompareItem(string custId, string techId)
        {
            CustId = custId;
            TechId = techId;
        }

        public int CompareTo(object obj)
        {
            CompareItem Temp = (CompareItem)obj;
            if (this.CustId != Temp.CustId || this.TechId != Temp.TechId)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
    }

    static void Main(string[] args)
    {
        List<CompareItem> LeftCompareSet = new List<CompareItem>();

        LeftCompareSet1.Add(new CompareItem("0000", "0001"));
        LeftCompareSet1.Add(new CompareItem("0001", "0001"));
        LeftCompareSet1.Add(new CompareItem("0002", "0002"));
        LeftCompareSet1.Add(new CompareItem("0003", "0003"));
        LeftCompareSet1.Add(new CompareItem("0002", "0004"));

        List<CompareItem> RightCompareSet = new List<CompareItem>();

        RightCompareSet1.Add(new CompareItem("0005", "0005"));
        RightCompareSet1.Add(new CompareItem("0004", "0004"));
        RightCompareSet1.Add(new CompareItem("0003", "0003"));
        RightCompareSet1.Add(new CompareItem("0002", "0002"));
        RightCompareSet1.Add(new CompareItem("0006", "0002"));

        int state = 0;

        List<int> tlc = new List<int>();
        List<int> trc = new List<int>();

        foreach (CompareItem lc in LeftCompareSet)
        {
            foreach (CompareItem rc in RightCompareSet)
            {
                state = lc.CompareTo(rc);
                if (state == 0)
                {
                    tlc.Add(0);
                }
                else
                { 
                    tlc.Add(1);
                }
            }

            if (tlc.Contains(1))
            {
                Console.WriteLine("Cust: " + lc.CustId + ", Tech: " + lc.TechId + ", Not missing");
            }
            else
            {
                Console.WriteLine("Cust: " + lc.CustId + ", Tech: " + lc.TechId + ", Missing");
            }
        }

        foreach (CompareItem rc in RightCompareSet)
        {
            foreach (CompareItem lc in LeftCompareSet)
            {
                state = rc.CompareTo(lc);
                if (state == 0)
                {
                    trc.Add(0);
                }
                else
                {
                    trc.Add(1);
                }
            }

            if (trc.Contains(1))
            {
                Console.WriteLine("Cust: " + rc.CustId + ", Tech: " + rc.TechId + ", Not missing");
            }
            else
            {
                Console.WriteLine("Cust: " + rc.CustId + ", Tech: " + rc.TechId + ", Missing");
            }
        }
    }

Upvotes: 0

Views: 181

Answers (3)

Red
Red

Reputation: 39

You just want to confirm that the items in first list is exist in second list ?

If I am right then you had taken a long road. just take an item from one list and check whether it is exist on second list or not

example :

secondList.Exists(first[0]);

it will return bool.

Upvotes: 0

FrankE
FrankE

Reputation: 313

I think there are several issues in this approach:

  1. the code contains a minor mistake: it must be LeftCompareSet instead of LeftCompareSet1

  2. In CompareTo you miss to check the type of the object, if the method is called with a wrong object you get an exception.

  3. I think what you really need is to implement the Equals method: public override bool Equals(object obj) (do not forget GetHashCode)

  4. Not sure what you really want to achieve, possibly a set operation is more helpful

  5. You didn't write what is wrong with the results, if I execute your code, I get the following result:

    • Cust: 0000, Tech: 0001, Missing
    • Cust: 0001, Tech: 0001, Missing
    • Cust: 0002, Tech: 0002, Not Missing
    • Cust: 0003, Tech: 0003, Not Missing
    • Cust: 0002, Tech: 0004, Not Missing
    • Cust: 0005, Tech: 0005, Missing

Upvotes: 0

zmbq
zmbq

Reputation: 39013

Your CompareTo is wrong. It should return 0 is the two objects are the same, -1 if one is smaller than the other and 1 if it's larger. See here

Upvotes: 2

Related Questions