Why am I getting an ""Object" does not contain a definition for "Index"" error?

3.4k Views Asked by At

I've looked and looked but I can't seem to find any solution to this problem so I decided I might as well ask.

I'm following this tutorial to help me create an address book in C# using WinForms. I've created the GUI, and the "Person" class that holds it all together, and I can add entries just fine. But when I write the code to switch between them, following what's written in the tutorial, I get this error message:

"object" does not contain a definition for "Index" and no extension method "Index" accepting a first argument of type "object" could be found (are you missing a using directive or an assembly reference?)

Here is my code for the "SelectedIndexChanged" event. It's slightly different from the tutorial: I'm putting "entryList.SelectedItems[0].Index" into a pre-defined int "i" to save writing it out eight times, my GUI is rather different, and I defined the list of Person objects in a separate "Methods" class for various reasons. The basic principle is the same though, so I don't see why mine flags up an error and his doesn't.

        private void entryList_SelectedIndexChanged(object sender, EventArgs e)
    {   //set the text boxes to display the data in the selected entry
        if (entryList.SelectedItems.Count > 0)
        {
            i = entryList.SelectedItems[0].Index;
            nameText.Text = Methods.list[i].Name;
            birthDay.Text = Methods.list[i].BirthDay.ToString();
            birthMonth.Text = Methods.list[i].BirthMonth.ToString();
            birthYear.Text = Methods.list[i].BirthYear.ToString();
            numberText1.Text = Methods.list[i].NumberPart1;
            numberText2.Text = Methods.list[i].NumberPart2;
            addressText.Text = Methods.list[i].Address;
            eMailText.Text = Methods.list[i].Email;
        }
    }

It's the ".Index" part that's flagging up the error and I don't know why. So, where have I gone wrong?

EDIT: Just to clarify, I have tried various workarounds such as

i = entryList.SelectedIndex;

but those still won't let me switch between the items in the ListBox, it keeps displaying whatever the last one I entered was.

EDIT 2: More info as requested by Rotem. Here's the code for adding an entry, this adds it to a list of Person objects which is kept in the separate Methods class for various reasons (hence Methods.list). It also adds the person's name to the ListBox list, and in theory, I then want to be able to switch between viewing different entries by clicking the names in the ListBox, which is what the first code segment is for. Except that's not working.

        private void newEntry_Click(object sender, EventArgs e)
    {   //creates a new entry, displaying its data in the appropriate fields, and then saves it
        Person p = new Person(nameText.Text, Convert.ToByte(birthDay.Text), Convert.ToByte(birthMonth.Text), Convert.ToInt16(birthYear.Text), numberText1.Text, numberText2.Text, addressText.Text, eMailText.Text);
        Methods.list.Add(p);
        entryList.Items.Add(p.Name);
        nameText.Text = " ";
        birthDay.Text = "DD";
        birthMonth.Text = "MM";
        birthYear.Text = "YYYY";
        numberText1.Text = " ";
        numberText2.Text = " ";
        addressText.Text = " ";
        eMailText.Text = " ";
    }

EDIT 3: As requested, here's the code for the "Person" class:

class Person
{
    private static string name;
    private static byte birthDay;
    private static byte birthMonth;
    private static short birthYear;
    private static string numberPart1;
    private static string numberPart2;
    private static string address;
    private static string email;

    //encapsulation goes here...

    public Person(string a, byte b, byte c, short d, string e, string f, string g, string h)
    {
        name = a;
        if (b <= 31 && c <= 12 && d < DateTime.Now.Year)
        {
            birthDay = b;
            birthMonth = c;
            birthYear = d;
        }
        else
            throw new ArgumentException("Invalid date of birth");
        numberPart1 = e;
        numberPart2 = f;
        address = g;
        email = h;
    }
}
2

There are 2 best solutions below

0
On

How about:

i = entryList.SelectedIndex

That should take the index from the listbox, as long as it matches your list you should be ok.

12
On

in entryList is a ListBox, then it's items are only objects, they are not some data structure that keeps track of the index (unlike, for example, ListView items, which have an Index property).

Instead, you can use the SelectedIndices property of the ListBox.

int i = entryList.SelectedIndices[0];