I have 3 classes: Foo, Bar and Jim.

public class Foo
    {
        public float a = float.MaxValue;
    }

    public class Bar
    {
        public float a = float.MaxValue;
    }

    public class Jim
    {
        public float a = float.MaxValue;
    }

I create a list that holds pairs of objects (classes in this case)

List<Pair> pairs = new List<Pair>(8);

    public class Pair
    {
        public object[] objects = new object[2];
    }

My method adds 2 objects to the list (in a pair) if a statement is true

Foo foo = new Foo();
            Bar bar = new Bar();
            Jim jim = new Jim();

            float number = 4;

            if (number < foo.a && number < bar.a)
            {
                foo.a = number;
                bar.a = number;

                Pair pair = new Pair();
                pair.objects[0] = foo;
                pair.objects[1] = bar;

                pairs.Add(pair);
            }

Later in the method another statement is true and involves an instance of a class already stored as a pair

if (number < foo.a && number < jim.a)
            {
                foo.a = number;
                jim.a = number;

                // Now I would like to replace any stored pair element which has the same foo instance.
            }

How can I make sure that the previously stored pair involving the instance of Foo is removed from the list, as this Foo instance is now involved with another pair?

How can this work for more than one instance of the Foo class, or any other class?

How about this?

List<Pair> pairs = new List<Pair>(8);

            Foo foo = new Foo();
            Bar bar = new Bar();
            Jim jim = new Jim();

            float number = 4;

            if (number < foo.a && number < bar.a)
            {
                foo.a = number;
                bar.a = number;

                Pair pair = new Pair();
                pair.objects[0] = foo;
                pair.objects[1] = bar;

                AddPair(pair, ref pairs);
            }

            number = 2;

            if (number < foo.a && number < jim.a)
            {
                foo.a = number;
                jim.a = number;

                Pair pair = new Pair();
                pair.objects[0] = foo;
                pair.objects[1] = jim;

                AddPair(pair, ref pairs);
            }

        private void AddPair(Pair pair, ref List<Pair> pairs)
        {
            for (int i = 0; i < pairs.Count; ++i)
            {
                if (pairs[i].objects[0] == pair.objects[0] ||
                    pairs[i].objects[0] == pair.objects[1] ||
                    pairs[i].objects[1] == pair.objects[0] ||
                    pairs[i].objects[1] == pair.objects[1])
                {
                    pairs[i] = pair;
                    return;
                }
            }

            pairs.Add(pair);
        }

That's the most efficient method that I can think of, but I welcome further advice.

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.