I have an object that i use in a generic list, i need to expose a property in that object that will return the index position of the object in the list plus one.

example

Items in list
myobject1
myobject2
myobject3

myobject2.indexValue would equal 1

any idea how I can acomplish this reliably?

Recommended Answers

All 4 Replies

This might help you:

public partial class Form1 : Form
    {
        List<MyObject> list;
        public Form1()
        {
            InitializeComponent();
            CreatingList();
            GetIndex();
        }

        private void CreatingList()
        {
            list = new List<MyObject>();
            MyObject m1 = new MyObject(1, "one");
            MyObject m2 = new MyObject(2, "two");
            list.Add(m1);
            list.Add(m2);
        }

        private void GetIndex()
        {
            int index1 = list.FindIndex(a => a.name == "two");
            int index2 = list.FindIndex(a => a.id == 1);
        }
    }

    class MyObject
    {
        public int id { get; set; }
        public string name { get; set; }

        public MyObject(int _id, string _name)
        {
            id = _id;
            name = _name;
        }
    }

I can easily get the index when i have a reference to the list. but from inside the list item I do not have a reference to the list.

public partial class Form1 : Form
    {
        List<MyObject> list;
        public Form1()
        {
            InitializeComponent();
            CreatingList();
            GetIndex();
        }

        private void CreatingList()
        {
            list = new List<MyObject>();
            MyObject m1 = new MyObject(1, "one");
            MyObject m2 = new MyObject(2, "two");
            list.Add(m1);
            list.Add(m2);
        }
    }

    class MyObject
    {
        public int id { get; set; }
        public string name { get; set; }

        public MyObject(int _id, string _name)
        {
            id = _id;
            name = _name;
        }

        public int MyIndex
        {
          get
            {
              return thisobjectsParentCollection.IndexOf(this);
            }
        }
    }

I guess I could create a custom IEnumerable object that added its self to a property of each object added to it. But I really don't want to do that.

Can anyone see another way?

This is becoming very philosophical.:-/
Who created me and where is my place in the order of things...

Do not know if this is possible at all, but reflection can perhaps help?

commented: Love the comment +3

in silverlight there is no collectionsbase, but the List<T> is inheritable. So i created an object that inherited from List<T> and replaced the add method with one that added itself to the objects add via a property I added to the type I strongly typed it the list to contain.

wow, that sounded complicated. Either way it worked. had an issue with serialization though, because this created a circular reference, but using the attribute [XMLIgnore()] I managed to have everything work the way i wanted it.

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.