Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C#
private bool ValidData()
        {
                 
            // Initialize function return value
            return false;
            
            // Test CustomerID is complete
            if (((service_NoTextBox.Text == String.Empty) 
                    ||(Information.IsNumeric(service_NoTextBox.Text) == false)))
            {
                // Required employee ID is not complete
                MessageBox.Show("Service Number is not complete", "Promotion List Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                service_NoTextBox.Focus();
                service_NoTextBox.SelectAll();
            }
            else if (((file_NoTextBox.Text == String.Empty)
                        || (Information.IsNumeric(file_NoTextBox.Text) == false)))
            {
                // Validate File Number
                MessageString = "File Number is required and must be in five digit format.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                file_NoTextBox.Focus();
                file_NoTextBox.SelectAll();
            }
            else if (((nameTextBox.Text == String.Empty)
                        || (Information.IsNumeric(nameTextBox.Text) == true)))
            {
                // Validate  Name
                MessageString = "Name is required in alphabet format.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                nameTextBox.Focus();
                nameTextBox.SelectAll();
            }
            else if (((new_RankTextBox.Text == String.Empty)
                        || (Information.IsNumeric(new_RankTextBox.Text) == true)))
            {
                // Validate New Rank
                MessageString = "New Rank is required in alphabet format.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                new_RankTextBox.Focus();
                new_RankTextBox.SelectAll();
            }
            else if (((old_RankTextBox.Text == String.Empty)
                        || (Information.IsNumeric(old_RankTextBox.Text) == true)))
            {
                // Validate Previous Rank
                MessageString = "Old Rank is required in alphabetic form.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                old_RankTextBox.Focus();
                old_RankTextBox.SelectAll();
            }
            else if (((stationTextBox.Text == String.Empty)
                        || (Information.IsNumeric(stationTextBox.Text) == true)))
            {
                // Validate State
                MessageString = "State is required in letter form.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                stationTextBox.Focus();
                stationTextBox.SelectAll();
            }
            else if (((comboBox1.Text == String.Empty)
                        || (Information.IsNumeric(comboBox1.Text) == true)))
            {
                // Command Is required
                MessageString = "Please Enter Command.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                comboBox1.Focus();
                comboBox1.SelectAll();
            }
            else if (((comboBox2.Text == String.Empty)
                        || (Information.IsNumeric(comboBox2.Text) == true)))
            {
                // Validate 
                MessageString = "State of Origin must be in Alphabet form.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                comboBox2.Focus();
                comboBox2.SelectAll();
            }
                
            else
            {
                // All of the data is valid
                return true;
            }
                
        }


What I have tried:

I have tried to set the ValidData = false and true, but i also get cannot assign to a method type
Posted
Updated 6-Apr-17 9:10am
v2

The first line of your method is return false;, so your method will immediately return false to the caller, and everything after this line will be ignored ("unreachable").

Only return false when you notice that the 'data' is actually invalid.

The comment above your 'return false' says 'Initialize function return value', so if that's what you want to do, then you can do something like bool returnValue = false; at the top, and at the end of the function, return returnValue; (and change returnValue in your method when necessary).
 
Share this answer
 
updated solution as per the comments from Dave Kreskowiak, removed the return keyword from each condition block.
replace it with a return variable and use it in required condition block and return the variable.

private bool ValidData()
       {

           // Initialize function return value
           bool returnValue = false;

           // Test CustomerID is complete
           if (((service_NoTextBox.Text == String.Empty)
           || (Information.IsNumeric(service_NoTextBox.Text) == false)))
           {
               // Required employee ID is not complete
               MessageBox.Show("Service Number is not complete", "Promotion List Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               service_NoTextBox.Focus();
               service_NoTextBox.SelectAll();
           }
           else if (((file_NoTextBox.Text == String.Empty)
           || (Information.IsNumeric(file_NoTextBox.Text) == false)))
           {
               // Validate File Number
               MessageString = "File Number is required and must be in five digit format.";
               MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               file_NoTextBox.Focus();
               file_NoTextBox.SelectAll();
           }
               .
               .
               .
               .

           else
           {

               returnValue = true;
           }
           return returnValue;

       }
   }
 
Share this answer
 
v3
Comments
Dave Kreskowiak 6-Apr-17 12:20pm    
It's bad practice to have more than one return statement in a method. It's horrible practice to have so damn many of them.
Karthik_Mahalingam 6-Apr-17 13:15pm    
Yes Dave you are right, i just focussed on clearing the error.
have updated the solution.
Richard Deeming 6-Apr-17 13:55pm    
That's another "tabs-vs-spaces" argument. I'm in favour of having multiple return statements if it makes the code cleaner. :)
Karthik_Mahalingam 6-Apr-17 21:40pm    
:)it depends on the scenario
Okezie Victor 6-Apr-17 15:17pm    
Thanks @Dave, Karthik and Richard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900