Instantiate list and Insert values at different times

Asked

Viewed 129 times

0

Hello, in a web application, I need to insert values in a list at different times. This insertion occurs after the user fills in fields on the page and gives the Submit in the form.

In Submit is called the method that inserts the values in the list.

In the method I have to instantiate the list to enter the values, but each time a new list is instantiated and I miss the previous one.

Follow the code I’m trying to formulate:

public static List<LanctoConferenciaCaixaModel> LanctoValorConferencia(string parDsFormaPagto, decimal parVlConferido)
{
    List<LanctoConferenciaCaixaModel> listLancamentoCaixa = null;

    if (listLancamentoCaixa == null)
    {
        listLancamentoCaixa = new List<LanctoConferenciaCaixaModel>();
    }

    listLancamentoCaixa.Add(new LanctoConferenciaCaixaModel()
    {
        dsFormaPgto = parDsFormaPagto,
        vlConferido = parVlConferido
    });

    return listLancamentoCaixa;
}

I believe my problem is in the if testing if the list is null.

PS: I started with C# programming only 3 months ago. :)

  • You are declaring a null list and then immediately checking that it is null. So, always block if shall be executed, and listLancamentoCaixa will be a new list.

  • list is attribute of some class? how do you use it outside of method?

  • 1

    @Cypherpotato, well observed, I copied the code from somewhere else and I didn’t notice it... but removing the null statement, in if it accuses "Use of local variable unassigned".

  • @Andersonnuernberg attributes an instance to it because then: var listLancamentoCaixa = new List<LanctoConferenciaCaixaModel>();

  • @Cypherpotato, then I fall exactly into my initial question, every time I call the method, it instantiates a list, and I lose the values already entered. if is would be exactly to see if there was already an instance of the list and add new values.

  • 1

    need to move the variable declaration to another scope, in the class for example, otherwise it will create again always since the list is a local variable, which is lost when processing the page. Try to move it to the class scope, outside the method

  • @Ricardopoint even moving out of the method, when testing if if is null, enters and creates a new instance, losing the values already informed.

  • this should happen pq every request is instantiated the page class again, you can solve this, just for this example (this is not the best way to do) declaring the variable as static static List<LanctoConferenciaCaixaModel> listLancamentoCaixa = null;. If you don’t know what a Static variable is, I suggest looking at the site

  • @Ricardopunctual, your tip to declare the variable as static worked in this case for what I need. Thank you.

Show 4 more comments

1 answer

0

Good morning. As the friends said, you are losing the contents of the list because it creates a new instance of it every execution of the method "Lanctovalorconference()".

I found this example here (Singleton X Static Classes - Macoratti) that can help you acquire more knowledge, but I also developed something thinking about the scenario you went through.

I ended up not testing the implementation, any doubt may ask.

/* Sua entidade */
public class LanctoConferenciaCaixaModel
{
  public string dsFormaPgto { get; set; }
  public decimal vlConferido { get; set; }

  public LanctoConferenciaCaixaModel(string formaPagto, decimal vlConferido)
  {
    dsFormaPgto = formaPagto;
    vlConferido = vlConferido;
  }
}

/* Esta classe guarda uma instancia da sua lista, desta maneira a mesma pode ser recuperada em diversos locais diferentes. */
public sealed class ConferenciaSingleton
{
  static ConferenciaSingleton _instancia
  public static ConferenciaSingleton Instancia { get return _instancia ?? (_instancia = new ConferenciaSingleton()); }

  private ConferenciaSingleton() {}
  public List<LanctoConferenciaCaixaModel> listLancamentoCaixa { get; set; }
}

/* Classe que adiciona novos lancamentos */
public List<LancamentoConferenciaCaixaModel> LanctoValorConferencia(string parDsFormaPagto, decimal parVlConferido)
{
  var variavelConferencia = ConferenciaSingleton.Instancia;

  var lancto = new LanctoConferenciaCaixaModel(parDsFormaPagto, parVlConferido);

  if (variavelConferencia.listLancamentoCaixa == null)
    variavelConferencia.listLancamentoCaixa = new List<LanctoConferenciaCaixaModel>();

  variavelConferencia.listLancamentoCaixa.Add(lancto);
}
  • Eduardo, I appreciate your immense help, but here in my project not right, but I understand your code and I’m sure to enjoy something in the future. The above suggestion of Ricardo worked right at this time for what I need to do... I will still perform more tests and develop the rest of the screen. If you need anything I’ll ask for help again. Thanks again.

Browser other questions tagged

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