Hello, i have a problem.

int index2 = 1;
        foreach(Circle circle in arrays)
        {
            if(index2 > 1 && index2 < 6)
            {
                Circle c1 = (Circle) arrays[index2 - 1];
                Circle c2 = (Circle) arrays[index2 - 2];

                Console.WriteLine("Radien på cirkel {0} är {1} och arean är {2}", index2, circle.getRadius(), circle.getArea());
                //This is written out first and then the line if the circles have same messures or not.

                if(c1.Equals(c2))
                {
                    Console.WriteLine("Cirkel {0} och {1} är lika stora.", index2 - 1, index2); // same messurements
                }
                else
                    Console.WriteLine("Cirkel {0} och {1} har olika mått.", index2 - 1, index2); // different messurements
            }

There are five objects in the array list. I want to compare objeck 2 with 1, 3 with 2 and so on.
In this for-loop the statement

if(c1.Equals(c2))

never comes true.
anyone knows why, then im very happy.

Recommended Answers

All 3 Replies

ok located the error it was when i was overloading the equalfunction.

public override bool Equals(object obj)
    {
        if (obj == null || this.GetType().Equals(obj.GetType()) || this.radius.Equals((obj.????????) )) return false;
        else return true;
    }

    public override int GetHashCode() 
    { 
        return base.GetHashCode(); 
    }

Now another one, how do i get the object to show the radius?
In hte code i marked it with ??????. What do I write there?
I tryed with obj.radius and so on also to cast obj to the type of the class now im getting annoyed...
Please help!

Put this above your if statement on line 3

Circle Mycircle = obj as Circle;

In the if statement use: . . . this.radius.Equals(Mycircle.radius) . . .
Must say, I have not tested it.

Yes!
Did it, thanks for your help.
Now code like this if someone is interested.

public override bool Equals(object obj)
    {
        if (obj == null || this.GetType().Equals(obj.GetType()) == false) return false;
        Circle c = (Circle)obj;
        if (this.radius.Equals(c.radius) == false) return false;
        else return true;
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.