Reputation: 65
I have a Array.It has for example 10 rows. I need to checka flg. if flag has value false it goes to array number one,if it have flag true it goes go array number 2.I'm trying something like this.
if (lista2[i].FLG_FALHA == true)
{
listaInc[c] = lista2[i];
i++;
c++;
}
else
{
listaAlr[o] = lista2[i];
o++;
i++;
}
This is where i declare the arrays.
List<AcompanhamentoSiltIncDTO> lista2 = new List<AcompanhamentoSiltIncDTO>();
List<AcompanhamentoSiltIncDTO> listaInc = new List<AcompanhamentoSiltIncDTO>();
List<AcompanhamentoSiltIncDTO> listaAlr = new List<AcompanhamentoSiltIncDTO>();
I get this error,it's like the arrays are not initialized.
"{"The index was out of range, it should be non-negative and less than the size of the collection. \ R \ nName parameter: index"}"
Upvotes: 0
Views: 81
Reputation: 19
You can solve this with LINQ.
var listaInc = from n in lista2
where n == true
select n;
var listaAlr = from n in lista2
where n == false
select n;
Bits more, bits less, but that is what I would do.
Upvotes: 0
Reputation: 1
You get this error because you got out of range of your array. Check out your indexes. But for this task I suggest you working with linq. It gives you a lot of good functionality.
And if you have "List<>" you need to add elements to this list with "Add" method. So code will be the next:
if (lista2[i].FLG_FALHA == true)
{
listaInc.Add(lista2[i]);
i++;
}
else
{
listaAlr.Add(lista2[i]);
i++;
}
But as I said you may use LinQ. Code will be the next:
listInc = lista2.Where(x => x.FLG_FALHA);
listAlr = lista2.Where(x => !x.FLG_FALHA);
Upvotes: 0
Reputation: 1175
You should call the Add()
method of your list:
if (lista2[i].FLG_FALHA == true)
listaInc.Add(lista2[i]);
else
listaAlr.Add(lista2[i]);
Otherwise, since your listaAlr
and listaInc
have no element, you get cannot access element at position o
: listaInc[o]
Upvotes: 2