How to add an element to a list?

Asked

Viewed 8,989 times

3

What is the correct syntax to call the method Add_Pessoa class ClsPessoa by a List?

public class ClsPessoa
{
    private static string PSTR_Nome = "";
    private static DateTime PDT_DataNascimento;
    private static string PSTR_Email = "";

    public ClsPessoa()
    {
    }

    static public string Nome
    {
        get { return PSTR_Nome; }
        set { PSTR_Nome = value; }
    }

    static public DateTime DataNascimento
    {
        get { return PDT_DataNascimento; }
        set { PDT_DataNascimento = value; }
    }

    static public string Email
    {
        get { return PSTR_Email; }
        set { PSTR_Email = value; }
    }       

    public void Add_Pessoa(string PV_Nome, DateTime PV_DataNascimento, string PSTR_Email)
    {
        Nome = PV_Nome;
        DataNascimento = PV_DataNascimento;
        Email = PSTR_Email;
    }

Something like that?

var pessoas = new List<ClsPessoa>();
pessoas[0].Add_Pessoa(TXT_Nome.Text, DTP_DataNascimento.DisplayDate, TXT_Email.Text);

1 answer

5


Don’t confuse the object Pessoa and the object List<Pessoa>. These are separate things. So you can’t "add a person to a person". It looks like the phrase doesn’t make sense. You can "add a person to a list of people". Then operations are separated.

If you are going to add, you do not choose in which position you will place. And just as in array, you have create an instance of the desired class and then add the instance:

var pessoas = new List<ClsPessoa>();
pessoas.Add(new ClsPessoa() {Nome = TXT_Nome.Text,
                             DataNascimento = DTP_DataNascimento.DisplayDate,
                             Email = TXT_Email.Text});

If you want to change the content of position 0 in the list then use:

pessoas[0] = new ClsPessoa() {Nome = TXT_Nome.Text,
                              DataNascimento = DTP_DataNascimento.DisplayDate,
                              Email = TXT_Email.Text});

Or if you want to move a property:

pessoas[0].Email = TXT_Email.Text;

But there’s a better way to build the whole class:

public class Pessoa {
    public Pessoa(string nome, DateTime dataNascimento, string email) {
        Nome = nome ?? ""; //esquisito, mas é o que tinha no código original
        DataNascimento = dataNascimento;
        Email = email ?? "";
    }
    public string Nome { get; set;}
    public DateTime DataNascimento { get; set; }
    public string Email { get; set; }
}

I put in the Github for future reference.

Looks like it got simpler. With better names, without using hungarian notation which is horrible and doesn’t help at all, without using anything out of standard. With simplified syntax of automatic properties. Using the constructor in the right way and avoiding an auxiliary method that was of no use. And mostly fixing the problem of static limbs. If you are going to create a class that needs to be instantiated, that is, you are going to create an object with data in it, the members of the object cannot be static.

You can still improve several things in this class. But let’s take parts. One of them would be not even having a builder. In this case it is not being especially useful. I just don’t say that it is completely unnecessary because I don’t know the requirement. I can see some reason to have it.

If you really need to have a class to better abstract a list of people, then another class needs to be created. But this is already another subject.

If you do this, then you can change the above syntax a little, although it won’t make much difference. I find the first form more readable. Although you can make the shape below more readable too. But leave it for later, I will not fill with news in your head.

pessoas.Add(new Pessoa(Nome.Text,
                       DataNascimento.DisplayDate,
                       Email.Text));

I know that I presented you several new things, do a search, if you find nothing here, ask a question about what you were left with doubt (one question per subject) and we will answer.

Learn more about static members on:

  • Just a small note: with this constructor, the properties Nome and DataNascimento should have the set private.

  • @I even think it’s a possibility. But not mandatory. It should only be private if you do not want to let the properties be changed publicly. In general, it is not what you want. Unless you want the object to be immutable. But then you’d also have to change a few more things.

  • Gee, thank you so much for the answer ... I understood perfectly what you mean ... = D made my learning much easier.

  • @Ayo if the bigown answer answers your question, feel free to accept it by clicking on the marker to the left of his answer.

  • @bigown Just one more question. Why use var instead of List<ClsPessoa> pessoas = new List<ClsPessoa>();

  • @Ayo Has a link in the reply talking in detail about it. If you follow all the links that I put, you will learn a lot of interesting things. Actually if you search here on the site you will find a lot that will help your learning. I especially like to answer things that teach people to program more than giving them a cake recipe. Check out my answers on tag C#, especially the most voted, and will open new questions whenever you have any questions that will arise during learning and not have here yet.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.