-4

I am trying to add new obj to to list class but is telling me i need obj reference to the property. I cant understand exactly what i should do, can you give me a tip ?

public class CreateContact
{
  logic...
}

public class AddedContacts
{
    private List<CreateContact> Contact;

    public List<CreateContact> ClassCreateContact
    {
        get { return Contact; }
        set { this.Contact = value; }
    }
}

I want to create and add to the list the new created "CreateContact" by clicking a button.

private void button4_Click(object sender, EventArgs e)
    {

        CreateContact p = new CreateContact(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
        AddedContacts.ClassCreateContact.add(p); // Error 1 An object reference is required for the non-static field, method, or property

    }
KRUGER88
  • 7
  • 1
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – walther Nov 04 '14 at 09:28
  • 1
    Nope, he's trying to access an instance property like a static one. – Dirk Nov 04 '14 at 09:29
  • 1
    Both are irrelevant links. Question is about how to access instance member :) – Sriram Sakthivel Nov 04 '14 at 09:29
  • You need to instantiate the Lists in your AddedContacts class's constructor! – TaW Nov 04 '14 at 09:30
  • possible duplicate of [An object reference is required for the non-static field, method, or property](http://stackoverflow.com/questions/3775721/an-object-reference-is-required-for-the-non-static-field-method-or-property) – Default Nov 04 '14 at 09:32

3 Answers3

0

You have defined a property in a class, but the property will only exist when an instance of the class exists. You need to create an instance via AddedContacts contacts = new AddedContacts(). Then contacts will be a reference to an actual object which contains a list.

If you want the class itself to contain a list, declare the property as static

Matthew Sainsbury
  • 1,390
  • 2
  • 17
  • 40
0

You need to instantiate the Lists in your AddedContacts class's constructor:

public class AddedContacts
{
    private List<CreateContact> Contact;

    public List<CreateContact> ClassCreateContact
    {
        get { return Contact; }
        set { this.Contact = value; }
    }

    public AddedContacts()
    {
        Contact = new List<CreateContact>();
        ClassCreateContact = new List<CreateContact>();
    }

}

You also need to create an instance of AddedContacts to work with and finally you need to watch case: It is Add not add:

 AddedContacts AC = new AddedContacts();
 AC.ClassCreateContact.Add(p);
TaW
  • 48,779
  • 8
  • 56
  • 89
0

If you will not use the setter

public class AddedContacts
{
    public readonly List<CreateContact> Contact = new List<CreateContact>();
}

private void button4_Click(object sender, EventArgs e)
{
    CreateContact p = new CreateContact(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
    AddedContacts ac= new AddedContacts();
    ac.Contact.Add(p);
}
serdar
  • 1,525
  • 1
  • 16
  • 29