adding data to class

Aman

New member
Joined
Oct 23, 2023
Messages
3
Programming Experience
1-3
C#:
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {

        public class class1
        {
            public List<Person> people { get; set; }
        }
        public class Person
        {
            public string name { get; set; }
            public string addess { get; set; }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Person p = new Person();
            p.name = "Test";
            p.addess = "Test 2";
            class1 data = new class1();
            data.people.Add(p);     // Error - object reference not set to an instance od an object

            MessageBox.Show(data.people[0].name);

        }
    }
}
 
Last edited by a moderator:
Please do not just dump code in post.

The title of a thread is supposed to be a summary of your problem. In body of the post, provide a more detailed explanation of what you are trying to do, what problem you are running into, and what you have done to try to solve the problem, and then present the code that has the problem.

People in this forum will typically invest time in helping you if you invest time in asking your question, and making it as easy as possible for others to try to help you. If it feels like you don't really care, it's more likely that others won't care either.

Anyway, to help you out: where is the people list instantiated? C# is not C++ where just declaring a class member instantiates that member when the class is instantiated.
 
Last edited:
sorry about that, @Skydiver

i will explain my problem, so i need to post json value by using api, i want to add values to class1 that has 1 array variable ( person )
can you help me solve that, how do i insert data into person from class1 ?
or do i have to declare person as variable and then put that to class1 ?
 
You are close. The most expedient way (but ignoring good object oriented programming principles), is to add a constructor to your class1 to instantiate the list; or directly instantiate the list after you instantiate the class1]. This is what we were alluding to in post #2 and #3.

If you were to follow good object oriented programming principles, then you should be following the "Law of Demeter", and so you should not be accessing the people member of class1 directly. Instead you should also have a AddPerson() method to class1.

 
In .NET, the convention is for collection properties to be read-only and instantiated internally. That means that, from the outside, you can get the existing collection and add or remove items but you cannot replace it with a whole new collection. That would usually be done like this:
C#:
public class Child
{
    // ...
}

public class Parent
{
    public List<Child> Children { get; } = new List<Child>();
}
The property is initialised where it's declared, so there's no need to write constructor code unless you want to add initial items. Even then, you could use an object initialiser and avoid constructor code if you didn't need input. The property has only a getter so it is read-only. It's a property, so its name begins with an upper-case letter.
 

Latest posts

Back
Top Bottom