0

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?

Edited by DaveTran: n/a

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by DaveTran
0

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.

Edited by DaveTran: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.