vendredi 3 juillet 2020

Checking input values with array contents in C#.net

I have an array with 5 integer values.

I need to check each value in it against input values from a text box.

A label displays a Value found message if the input value exists in the array. If not, theValue not found message is displayed.

I'm having trouble figuring out what condition to use and where, in order to display the right message at the right time.

Here's my code,

 private void button1_Click(object sender, EventArgs e)
 {
        int[] id = { 1, 2, 3, 4, 5 };
        int maxId = 5;
        
        try
        {
            int x = Convert.ToInt32(txtid.Text);
            int row;

            for (row = 0; row < maxId; row++)
            {
                if (id[row] == x)
                {
                    txtdesc.Text = "Value found!";
                }
            }

            txtdesc.Text = "Value not found!";
            
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
 }   

Aucun commentaire:

Enregistrer un commentaire