none
Boolean method RRS feed

  • Question

  • I'm trying to write a Boolean method to perform validations. For example....

     

     

    public bool validation(textbox textbox1, textbox textbox2, textbox textbox3)
    {
    	bool result = true;
    	
    	textbox[] txtFields = new textbox[3] { textbox1, textbox2, textbox3 };
    	string[] strFields = new string[3] { textbox1.Text, textbox2.Text, textbox3.Text };
    	
    	for (var i = 0; i <= txtFields.Count && i <= strFields.Length; i++)
    	{
    		if ((string)IsStringEmpty(strFields[i]))
    		{
    			txtFields[i].background = Color.Red;
    			result = false;
     } else { txtFields[i].background = Color.White; } } return result; }

     

    Then i would like to wright some code that say's if that method return true  continue and if not stop code.Im thinking it would look like this.

     

    if (validation())
    
    {
    
          //// do something
    
    }
    



    • Edited by uknowmeim Thursday, September 15, 2011 3:18 PM
    Thursday, September 15, 2011 3:13 PM

Answers

  • If you only wanna do some checking if controls are empty (no text inside), and setting background color, then you can do something like this:

     

            private void button2_Click(object sender, EventArgs e)
            {
                TextBox[] tbs = { textBox1, textBox2, textBox3 };
                bool bChecking = TextBoxesValidation(tbs);
                if (!bChecking)
                    MessageBox.Show("Some of the textBoxes are empty. Fil red textBoxes before continuing.");
                else
                {
                    //all ok..
                }
            }
    
            private bool TextBoxesValidation(TextBox[] tbs)
            {
                bool result = true;
                for (int i = 0; i < tbs.Length; i++)
                {
                    if (String.IsNullOrEmpty(tbs[i].Text))
                    {
                        tbs[i].BackColor = Color.Red;
                        result = false;
                    }
                    else
                        tbs[i].BackColor = Color.White;
                }
                return result;
            }
    



    Mitja
    • Proposed as answer by Link.fr Thursday, September 15, 2011 4:45 PM
    • Marked as answer by Lie You Monday, September 19, 2011 5:51 AM
    Thursday, September 15, 2011 3:28 PM

All replies

  • This code seems to be working fine. I just dont know why would you like to return boolan value? It seems pointless.

    You actually did write any quesion.


    Mitja
    • Proposed as answer by Link.fr Thursday, September 15, 2011 4:45 PM
    Thursday, September 15, 2011 3:19 PM
  • If you only wanna do some checking if controls are empty (no text inside), and setting background color, then you can do something like this:

     

            private void button2_Click(object sender, EventArgs e)
            {
                TextBox[] tbs = { textBox1, textBox2, textBox3 };
                bool bChecking = TextBoxesValidation(tbs);
                if (!bChecking)
                    MessageBox.Show("Some of the textBoxes are empty. Fil red textBoxes before continuing.");
                else
                {
                    //all ok..
                }
            }
    
            private bool TextBoxesValidation(TextBox[] tbs)
            {
                bool result = true;
                for (int i = 0; i < tbs.Length; i++)
                {
                    if (String.IsNullOrEmpty(tbs[i].Text))
                    {
                        tbs[i].BackColor = Color.Red;
                        result = false;
                    }
                    else
                        tbs[i].BackColor = Color.White;
                }
                return result;
            }
    



    Mitja
    • Proposed as answer by Link.fr Thursday, September 15, 2011 4:45 PM
    • Marked as answer by Lie You Monday, September 19, 2011 5:51 AM
    Thursday, September 15, 2011 3:28 PM
  • I think your code is ok.
    Hasibul Haque,MCC,MCPD blog.e-rains.com
    • Proposed as answer by Link.fr Thursday, September 15, 2011 4:45 PM
    Thursday, September 15, 2011 3:29 PM
  • Yah, You're in a right track..
    - <BooM>
    • Proposed as answer by Link.fr Thursday, September 15, 2011 4:45 PM
    Thursday, September 15, 2011 3:38 PM
  • Your code is correct.

    aelassas.free.fr
    • Proposed as answer by Link.fr Thursday, September 15, 2011 4:45 PM
    Thursday, September 15, 2011 3:45 PM
  • Thank you all for the support and encouragement and thank you Mitja Bonca for providing a simpler way to write it.
    Thursday, September 15, 2011 4:18 PM