in my code when I search for pre-inserted variables it shows the variable more than once

Asked

Viewed 37 times

-1

for some reason as soon as I search for a variable in the code it lists the variable only that more than once based on the Count variable,

int x=0;
int count=0;
string[] item = new string[500];
private void btn_Enter_Click(object sender, EventArgs e)
        {
            if (op == true)//op é um boolean que eu usei pra identificar se ele ira inserir variaveis ou pesquisar por elas
            {
                int y = 1;
                count += 1;
                for (x=0;x< y; x++)
                {
                    item[count] = textbox.Text;//aqui ele insere as variaveis em um list box
                    listBox1.Items.Add(item[count]);
                }
            }
            if (op == false)
            {
                for (x = 0; x <= count; x++) {
                    if (textbox.Text == item[count]) {
                        listBox2.Items.Add(item[count]);//aqui era pra ele pesquisar as variaveis
                    }
                }
            }
         }
  • for q um for q only runs 1 time ?

1 answer

0

Exchange Count for x, the x that is serving as index.

Each array has an index, which defines its position in the list, this index starts with 0:

for (x=0;x< y; x++) { 
   item[x] = textbox.Text; //Passando o x como índice da sua variável, para o texto box pegar ele 
   listBox1.Items.Add(item[x]); 
}

In the second example the same thing:

for (x = 0; x <= count; x++) { 
  if (textbox.Text == item[x]) { 
    listBox2.Items.Add(item[x]);//aqui era pra ele pesquisar as variáveis agora pelo indice 
  } 
}

In your second structure is why did you put Count as a parameter? It is zero.

Browser other questions tagged

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