Feed from the list c#

Asked

Viewed 107 times

1

I have a list in my code that is fed as follows:

string Recursos = "E-mail, Desktop, Datasul, Aptus";
            string[] RecursoArray = Regex.Split(Recursos, @"\W+\s+");

            List<Ti01> ListaRecursos = new List<Ti01>();

            Ti01 t1 = new Ti01();

            foreach (var tarefa in RecursoArray)
            {


                if (!string.IsNullOrEmpty(tarefa))
                {

                    t1.Recursos = tarefa;

                    if (tarefa.Equals("Datasul") || tarefa.Equals("Protheus") || tarefa.Equals("Aptus") || tarefa.Equals("SalesForce"))
                    {
                        t1.TipoRecurso = "Sistemas";
                    }
                    else
                    {
                        t1.TipoRecurso = "Infra";
                    }


                    if (tarefa.Equals("Datasul"))
                    {
                        t1.ObsRecurso = model.ObsDatasul;
                    }
                    else if (tarefa.Equals("Protheus"))
                    {
                        t1.ObsRecurso = model.ObsProtheus;
                    }
                    else
                    {
                        t1.ObsRecurso = "";
                    }

                    ListaRecursos.Add(t1);

                }

But only the ULTIMO appeal to that foreach.

In the case of the example, the resource Aptus would be added to that list 4 vezes, ignoring all other indexes. Does anyone have a sense of what might be going on?

  • 1

    Add the Ti01 t1 = new Ti01(); Within the foreach

1 answer

4


The problem is that you only create an instance of the object Ti01() and keeps inserting it several times, just changing the information. The keyword new has the role of creating a new object (or new reference) of the target class. As you are only giving a new, it’s like they’re all the same object. For example:

Pessoa pessoa = new Pessoa();
pessoa.idade = 20;
List<Pessoa> listaPessoas = new List<Pessoa>();
listaPessoas.add(pessoa);
pessoa.idade = 22; //Altera o objeto pessoa que está na lista
listaPessoas.add(pessoa); //Adiciona mais uma referência ao mesmo objeto pessoa

So it seems that he only added the last object, but the truth is that he only added several times the reference to that same object and changing the values of the referenced object, giving the feeling that he only added the last one. To solve this problem, you need to instantiate a new object (give a new) every iteration of the loop, for example:

Pessoa pessoa = new Pessoa();
pessoa.idade = 20;
List<Pessoa> listaPessoas = new List<Pessoa>();
listaPessoas.add(pessoa);
pessoa = new Pessoa(); //Cria um novo objeto pessoa
pessoa.idade = 22;
listaPessoas.add(pessoa); //Adiciona uma nova referência com outra idade

In the case of your code, you only need to place the line:

Ti01 t1 = new Ti01();

Inside the loop:

foreach (var tarefa in RecursoArray)
  • 1

    That’s right. I removed the foreach object to implement new functions, and I ended up not paying attention to it. Thanks for the help!!

Browser other questions tagged

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