简体   繁体   中英

C# - Verify List

So I have this code to verify if the 4 colors (array "cores_dos_pontos_medios") appears 3 straight frames (from video feed). If it does I can do:

posicao = loc.Coordenadas(cores_dos_pontos_medios); //obtem as coordenadas atraves das cores

The problem is that it's not verifying correctly. It still sends cores_dos_pontos_medios to loc even tho it doesnt appear 3 times straight.

cores = cores_dos_pontos_medios[0] + " , " + cores_dos_pontos_medios[1] + " , " + cores_dos_pontos_medios[2] + " , " + cores_dos_pontos_medios[3];

int n_de_verificacoes_cores = 3;

if (lista_cores.Count >= n_de_verificacoes_cores)
{
    lista_cores.RemoveAt(0);
}
lista_cores.Add(cores);


if (lista_cores.Count >= n_de_verificacoes_cores && lista_cores.Any(s => s == lista_cores[0]))
{

    posicao = loc.Coordenadas(cores_dos_pontos_medios); //obtem as coordenadas atraves das cores

EDIT: now that I think about it maybe I need to remove everything on the list lista_cores?

The problem is that lista_cores.Any(s => s == lista_cores[0]) is always true because this instruction is checking if any of the element of lista_cores is equal to the first element of lista_cores. If what you want to do is check if all elements are equal you have to use lista_cores.All(s => s == lista_cores[0])

From your " I wanted to compare if all the string on that list have the same text " comment I suppose you want something like this, but I didnt understand most of your question..

bool containsInvalidData = false;
foreach(string valueToCheck in lista_cores)
{
    if(lista_cores.Any(p => valueToCheck != p)) containsInvalidData = true;
    break;
}

You can also do it in a single call, using List.ForEach() function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM