How do I add an element to a list?

3

What is the correct syntax for calling the Add_Pessoa method of the ClsPessoa class for 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 this?

var pessoas = new List<ClsPessoa>();
pessoas[0].Add_Pessoa(TXT_Nome.Text, DTP_DataNascimento.DisplayDate, TXT_Email.Text);
    
asked by anonymous 11.10.2015 / 22:29

1 answer

4

Do not confuse the object Pessoa and the object List<Pessoa> . They are different things. So you can not "add a person into a person". Look how the sentence does not make sense. You can "add a person to a list of people". Then the operations are separated.

If it will add, you do not choose which position you are going to place. And just like in array , you have to 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 there would use:

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

Or if you want to change 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; }
}

Look how simple it got. With better names, without using Hungarian notation that is horrible and does not help at all, without using nonstandard things . With simplified syntax of automatic properties . Using the builder in the right way and avoiding a helper method that was useless. And mostly fixing the problem of static members. If you are going to create a class that needs to be instantiated, ie it will create an object with data in it, the object's members can not be static.

There are still a number of things to improve in this class. But let's go in parts. One of them would be to have a builder. In this case it is not being particularly useful. Just do not say it's completely unnecessary because I do not know the requirement. I can see some reason to have it.

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

If you do this, you can change the syntax above, but it will not make much difference. I find the first form more readable. Although give to let the form below more readable as well. 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 I've introduced you to a lot of new things, check it out, do not find anything here, ask a question about what's left in doubt (one question per subject) and we'll respond.

Learn more about static members at:

11.10.2015 / 22:50