سؤال

I want to create objects with 5 properties and each properties has 2 attributes. After that, I compare the objects if they are same, they will be grouped in same category.

Here is the code:

Item.cs

public class Item
{
    public Item()
    {
    }

    public SortProperty SortPropA { get; set; }
    public SortProperty SortPropB { get; set; }
    public SortProperty SortPropC { get; set; }
    public SortProperty SortPropD { get; set; }
    public SortProperty SortPropE { get; set; }

    public string Name { get; set; }
    public string Desc { get; set; }
}

SortProperty.cs

public class SortProperty : IEquatable<SortProperty>
{
    public string PartName { get; set; }
    public string GroupabilityID { get; set; }

    public SortProperty()
    {
    }

    public override int GetHashCode()
    {
        int hash = 19;
        hash = hash * 31 + (GroupabilityID == null ? 0 : GroupabilityID.GetHashCode());
        hash = hash * 31 + (PartName == null ? 0 : PartName.GetHashCode());
        return hash;
    }

    public bool Equals(SortProperty obj)
    {
        return (obj == null) ?
           false : (GroupabilityID == obj.GroupabilityID) || (PartName == obj.PartName);
    }

    public override bool Equals(Object obj)
    {
        SortProperty itemobj = obj as SortProperty;
        return itemobj == null ? false : Equals(itemobj);
    }

}

Program.cs (main class to test the coding)

class Program
{
    static void Main(string[] args)
    {
        Item objA = new Item();
        Item objB = new Item();

        // ------ Object A
        objA.Name = "Card1";
        objA.Desc = "Product Test A";
        //Property A
        objA.SortPropA = new SortProperty();
        objA.SortPropA.PartName = "Plastic A";
        objA.SortPropA.GroupabilityID = "A1";
        //Property B
        objA.SortPropB = new SortProperty();
        objA.SortPropB.PartName = "Color Green";
        objA.SortPropB.GroupabilityID = "B2";
        //Property C
        objA.SortPropC = new SortProperty();
        objA.SortPropC.PartName = "Visa";
        objA.SortPropC.GroupabilityID = "C1";

        // ------ Object B
        objB.Name = "Card2";
        objB.Desc = "Product Test B";
        //Property A
        objB.SortPropA = new SortProperty();
        objB.SortPropA.PartName = "Plastic B";
        objB.SortPropA.GroupabilityID = "A2";
        //Property B
        objB.SortPropB = new SortProperty();
        objB.SortPropB.PartName = "Color Lime";
        objB.SortPropB.GroupabilityID = "B1";
        //Property C
        objB.SortPropC = new SortProperty();
        objB.SortPropC.PartName = "Visa";
        objB.SortPropC.GroupabilityID = "C1";

        bool isEqual = objA.Equals(objB);

        if (isEqual == true)
            Console.WriteLine("Is same");
        else
            Console.WriteLine("Is different");

        Console.ReadKey();
    }
    }

The result should return true because there is a same property between objA and objB (SortPropc) but it return false.

I believe I have miss some logic part and I have sitting on chair for 4 hours but couldn't fix it. Can anyone please solve it?

هل كانت مفيدة؟

المحلول

The result should return true because there is a same property between objA and objB (SortPropc) but it return false.

You have just not implemented it. Read your code again and try to find the piece where you actually compare two Item instances. There's is none.

You should implement an Equals and GetHashCode method on your Item class, something like this:

public override bool Equals(Object obj)
{
    var o = (Item)obj;
    // Note: not error checking :-)
    return SortPropA.Equals(o.SortPropA) || 
           SortPropB.Equals(o.SortPropB) || 
           SortPropC.Equals(o.SortPropC) || 
           SortPropD.Equals(o.SortPropD) || 
           SortPropE.Equals(o.SortPropE);
}

or create a class that implements IEqualityComparer<Item> that handles this requirement.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top