Find item in a List with Contains method

Asked

Viewed 8,466 times

2

I have a Data object, which has the attributes name(string) and transactBlock(int). I have a Data List called dataBlock. I want to see if this list has an item already with the same name and transaction as the one passed as parameter. If it does not have add, if it does not have add. I’m trying to use the contains method, but it’s not working. It adds the item even though it already has one. What might be happening?

Data Class

public class Dado
    {
        private string nome;
        private int transacaoBloqueio;

        public string getNome()
        {
            return this.nome;
        }

        public int getTransacaoBloqueio()
        {
            return this.transacaoBloqueio;
        }

        public void setNome(string nomeDado)
        {
            this.nome = nomeDado;
        }

        public void setTransacaoBloqueio(int transacao)
        {
            this.transacaoBloqueio = transacao;
        }
    }


private List<Dado> dadosBloqueioCompartilhado= new List<Dado>();
private Dado dadoAtual = new Dado();
dadoAtual.setNome('A');
dadoAtual.setTransacaoBloqueio(1);

if (!dadosBloqueioCompartilhado.Contains(dadoAtual))
{
    dadosBloqueioCompartilhado.Add(dadoAtual);
}
  • 1

    Put the given class and dataBlock Share too please! Already anti hand help so it doesn’t really work because the classes are different by the instances created! You will have to do otherwise! because you have to test the value! but, put the two classes I’m formulating an answer

  • @João Colocar a classe Dado. The dataBloqueioCompartilh is a Data List.

  • I’ve already worked out the answer. I’ll put a note on your class, it seems to use Java to set and get as property in the NET world is not like this!

1 answer

5


When you create a classe and a lista dessa classe and tries to verify if a value coexists within that list there can be confusion in the method Contains that checks the instance of the class and not the actual value of the properties.

Example:

Class Dado

public class Dado
{
   public string Nome { get; set; }
   public int TransacaoBloqueio { get; set; }
}

Code 1:

IList<Dado> dadosBloqueioCompartilhado = new List<Dado>();

Dado dadoAtual = new Dado();
dadoAtual.Nome = "A";
dadoAtual.TransacaoBloqueio = 1;

if (!dadosBloqueioCompartilhado.Contains(dadoAtual))
{
    dadosBloqueioCompartilhado.Add(dadoAtual);
}

if (!dadosBloqueioCompartilhado.Contains(dadoAtual))
{
    dadosBloqueioCompartilhado.Add(dadoAtual);
}

In this code snippet the first if will let you add the item inside the list, the second no, because the instance of classe already exists within that list is the method Contains checks the object (the instance) and not its values.

Code 2:

IList<Dado> dadosBloqueioCompartilhado = new List<Dado>();

Dado dadoAtual = new Dado();
dadoAtual.Nome = "A";
dadoAtual.TransacaoBloqueio = 1;

if (!dadosBloqueioCompartilhado.Contains(dadoAtual))
{
    dadosBloqueioCompartilhado.Add(dadoAtual);
}

dadoAtual = new Dado();
dadoAtual.Nome = "A";
dadoAtual.TransacaoBloqueio = 1;

if (!dadosBloqueioCompartilhado.Contains(dadoAtual))
{
    dadosBloqueioCompartilhado.Add(dadoAtual);
}

In this specific case you will get an unwanted surprise, because, the list will have added the two items, because, its instances are different and the Contains then checks the instances and not the values.

Code 3

Apparent solution for verifying the continuous data in the class:

Create a class by implementing the interface Iequalitycomparer:

public class ComparerDados : IEqualityComparer<Dado>
{
    public bool Equals(Dado x, Dado y)
    {
        return (x.Nome.Equals(y.Nome)) &&
            (x.TransacaoBloqueio.Equals(y.TransacaoBloqueio));
    }

    public int GetHashCode(Dado obj)
    {
        return 0;
    }
}

And with the code below it will check the data contained in the class instance with the method Contains, by checking the values contained in the class instance:

IList<Dado> dadosBloqueioCompartilhado = new List<Dado>();

Dado dadoAtual = new Dado();
dadoAtual.Nome = "A";
dadoAtual.TransacaoBloqueio = 1;

if (!dadosBloqueioCompartilhado.Contains(dadoAtual, new ComparerDados()))
{
    dadosBloqueioCompartilhado.Add(dadoAtual);
}

dadoAtual = new Dado();
dadoAtual.Nome = "A";
dadoAtual.TransacaoBloqueio = 1;

if (!dadosBloqueioCompartilhado.Contains(dadoAtual, new ComparerDados()))
{
    dadosBloqueioCompartilhado.Add(dadoAtual);
}

Then you’ll only have one item on the list.


Observing: Nothing prevents you from using setNome, but the ideal and standard of the architecture . NET is public string Nome {get ;set; } Besides being more readable is the standard for any and all code. Example if do it the way setNome in the ORM Entity Framework he will not consider.

  • 3

    Perfect! Tested and worked. Thank you for helping John. Great and enlightening answer.

  • 1

    What if I want to return the data he found on the list? How can I do?

  • 1

    You can say more I really did not understand. Says so the process.?

Browser other questions tagged

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