title: Why am I getting an ""Object" does not contain a definition for "Index"" error?
body: <p>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.</p>
  
  <p>I'm following <a href="http://www.youtube.com/watch?v=0ZoDmeAZAD8" rel="nofollow">this tutorial</a> 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:</p>
  
  <p>"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?)</p>
  
  <p>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.</p>
  
  <pre><code>        private void entryList_SelectedIndexChanged(object sender, EventArgs e)
      {   //set the text boxes to display the data in the selected entry
          if (entryList.SelectedItems.Count &gt; 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;
          }
      }
  </code></pre>
  
  <p>It's the ".Index" part that's flagging up the error and I don't know why. So, where have I gone wrong?</p>
  
  <p>EDIT: Just to clarify, I have tried various workarounds such as</p>
  
  <pre><code>i = entryList.SelectedIndex;
  </code></pre>
  
  <p>but those still won't let me switch between the items in the ListBox, it keeps displaying whatever the last one I entered was.</p>
  
  <p>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.</p>
  
  <pre><code>        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 = " ";
      }
  </code></pre>
  
  <p>EDIT 3: As requested, here's the code for the "Person" class:</p>
  
  <pre><code>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 &lt;= 31 &amp;&amp; c &lt;= 12 &amp;&amp; d &lt; 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;
      }
  }
  </code></pre>