Adding more than one string to a List

Asked

Viewed 720 times

0

I have a customer registration system and I am trying to add products to registered customers. For the registration list I have the following:

public class Cliente
{
    public string Nome { get; set; }
    public string CPF { get; set; }
    public List<ProdutoComprado> Comprou { get; set; }
}

public class ProdutoComprado
{
    public string NomeProduto { get; set; }
    public string ValorProduto { get; set; }
}

public static List<Cliente> Clientes = new List<Cliente>();
public static List<ProdutoComprado> ProdutosComprados = new List<ProdutoComprado>();

To register the customer, I use the following code:

private void btnCadastrarCliente_Click(object sender, EventArgs e)
    {
        Cliente cliente = new Cliente();
        cliente.Nome = caixaClienteNome.Text;
        cliente.CPF = caixaClienteCPF.Text;
        listaClientes.Items.Add(cliente.Nome);
        Clientes.Add(cliente);
    }

To register a product for the customer I use:

private void btnCadastrarProduto_Click(object sender, EventArgs e)
    {
        Cliente procli = Clientes[listaClientes.SelectedIndex];
        procli.Comprou = new List<ProdutoComprado>
        {
            new ProdutoComprado
            {
                NomeProduto = caixaProdutoComprado.Text,
                ValorProduto = caixaValor.Text
            }
        };
        foreach (var prod in procli.Comprou)
        {
            ProdutosComprados.Add(prod);
            listaProdutosComprados.Items.Add(prod.NomeProduto);
        }
    }

Note that the code uses as matrix the client selected in the customers listbox, so that the product is added to it.
I also use JSON.net to save the data, and I have as output the following:

[
    {
        "Nome": "João",
        "CPF": "00000000000",
        "Comprou": [
                       {
                           "NomeProduto": "Carro 4x4",
                           "ValorProduto": "R$40000"
                       }
                   ]
    }
]

The problem is that this code only adds 1 product. If I select the customer and register another product for it, the product instead of being added simply overwrites the previous one.
Instead of saving in this way, for example:

[
    {
        "Nome": "João",
        "CPF": "00000000000",
        "Comprou": [
                       {
                           "NomeProduto": "Carro 4x4",
                           "ValorProduto": "R$40000",
                           "NomeProduto": "Moto Honda",
                           "ValorProduto": "R$20000"
                       }
                   ]
    }
]

..it saves so by overwriting the previous:

[
    {
        "Nome": "João",
        "CPF": "00000000000",
        "Comprou": [
                       {
                           "NomeProduto": "Moto Honda",
                           "ValorProduto": "R$20000"
                       }
                   ]
    }
]

How to add one product followed by another?

  • I was reading for a long time, trying to interpret and I couldn’t understand what your code does and what’s wrong with it. Unless I’m making a mistake in something it’s probably missing relevant parts of code or a better explanation.

  • @Bigown Thanks for your comment! I did not insert another part of this code so there would be too much information, which would hinder the resolution of the problem. This other part consists of registering the customer. I updated the question and ask you to take a look.

  • Customer registration really seems to be irrelevant. JSON helps you understand a little but I don’t know what part of your code is dealing with it. For example, I don’t know what it is caixaProdutoComprado.Text, not what is listaProdutosComprados just p/ cite two information that is there as magic. in fact the whole code is difficult to identify what it does. But clearly the part of the code that uses that caixaProdutoComprado.Text is taking only one product, wherever this comes from. The problem is basically there. You have to take others, but just seeing this is not helping.

  • @Box bigown Productproduct.Text is the text of a textbox. productlistComprados is a listbox where the customer’s products should be added. The product name comes from the textbox boxProductComprado at the moment the user presses the product registration button. My problem is I can’t add more than one product. As I said in the question, if there is already a product, when clicking the sign-up button to register a new one, instead of it being added as I exemplified in JSON, it overwrites the previous one.

  • @bigown can not specify all products at once because they should be added separately.

1 answer

3

As pointed out in the comments, your code is very difficult to understand but something caught my attention as potential problem:

procli.Comprou = new List<ProdutoComprado>
{
    new ProdutoComprado
    {
        NomeProduto = caixaProdutoComprado.Text,
        ValorProduto = caixaValor.Text
     }
};

You are always generating a new list, the ideal would just call:

procli.Comprou.Add(new ProdutoComprado
{
    NomeProduto = caixaProdutoComprado.Text,
    ValorProduto = caixaValor.Text
});

This way you will always be adding to the list.

Edit:

As pointed out in the comments of this answer, you need to create the list before calling the "Add" method. Ideally you do this in the Customer class builder:

public class Cliente
{
    public Cliente()
    {
        Comprou = new List<ProdutoComprado>();
    }

    public string Nome { get; set; }
    public string CPF { get; set; }
    public List<ProdutoComprado> Comprou { get; set; }
}
  • 1

    A detail: the list will have to be created when the Client class is created, otherwise NullReferenceException.

  • Well observed @Omni. It even crossed my mind, but I hypothesized that he was repeating the new within the Client class constructor. That’s what I would possibly do.

  • we are two to do equally. Anyway, I think it’s better to play it safe.

Browser other questions tagged

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