Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C#
public bool ValidCompulsoryFields()
    {
        if (ItemTypeName.Text == string.Empty || ItemTypeName.Text == StringConstants.select ||
                    ItemTypeName.SelectedValue.ToString() == StringConstants.select)
        {
            this.Master.LblError.Text= ErrorConstants.selectItemType;
            this.Master.LblError.ForeColor = Color.Red;
            ItemTypeName.Focus();
            return false;
        }

        if (ItemCategoryName.Text == string.Empty || ItemCategoryName.Text == StringConstants.select ||
                    ItemCategoryName.SelectedValue.ToString() == StringConstants.select)
        {
            this.Master.LblError.Text=ErrorConstants.selectItemCat;
            this.Master.LblError.ForeColor = Color.Red;
            ItemCategoryName.Focus();
            return false;
        }

        if (ManufacturerName.Text == string.Empty || ManufacturerName.Text == StringConstants.select ||
               ManufacturerName.SelectedValue.ToString() == StringConstants.select)
        {
            this.Master.LblError.Text=ErrorConstants.selectMake;
            this.Master.LblError.ForeColor = Color.Red;
            ManufacturerName.Focus();
            return false;
        }

        if (PackagingByName.Text == string.Empty || PackagingByName.Text == StringConstants.select ||
               PackagingByName.SelectedValue.ToString() == StringConstants.select)
        {
           this.Master.LblError.Text=ErrorConstants.selectPackage;
           this.Master.LblError.ForeColor = Color.Red;
                
PackagingByName.Focus();
           return false;
        }
        else
            return true;
Posted

Assuming that "ItemTypeName", "ItemCategoryName" and so forth are all text boxs or similar, refactor it to a method:
private bool CheckValidity(TextBox tb, ErrorConstant fail)
   {
   if (tb.Text == string.Empty || tb.Text == StringConstants.select ||
               tb.SelectedValue.ToString() == StringConstants.select)
       {
       this.Master.LblError.Text= fail;
       this.Master.LblError.ForeColor = Color.Red;
       tb.Focus();
       return false;
       }
   return true;
   }
Then just call it repeatedly:
return CheckValidity(ItemTypeName,ErrorConstants.selectItemType) &&
       CheckValidity(ItemCategoryName,ErrorConstants.selectItemCat) &&
       CheckValidity(ManufacturerName,ErrorConstants.selectMake) &&
       CheckValidity(PackagingByName,ErrorConstants.selectPackage);

"But here, I've Four different textboxes.
Then how can I take them in one 'If Statement'?

actually i'm having 4 combo boxes not textboxes"


TextBox/ComboBox - it makes no difference! Just change the parameter type.

When you do a statement such as
bool b = bool1 && bool2 && bool3;
it evaluates them in strict order: Bool1 first. If this is true, evaluate bool2. If it isn't true, go no further because the whole expression is false.
As soon as the compiler gets to the point where a single false is encountered, it stops processing.
So:
return CheckValidity(ItemTypeName,ErrorConstants.selectItemType) &&
       CheckValidity(ItemCategoryName,ErrorConstants.selectItemCat) &&
       CheckValidity(ManufacturerName,ErrorConstants.selectMake) &&
       CheckValidity(PackagingByName,ErrorConstants.selectPackage);
will return true if and only if all of your TextBoxes / ComboBoxes pass the validation. If any one of them fails, it will stop processing, and return false;
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Apr-11 4:14am    
Looks not bad, my 5.
I decided to stay away from the rat race, gave a general advice, what do you think?
--SA
Manfred Rudolf Bihy 18-Apr-11 4:21am    
Exactly what I had in mind. Except that my first post got somehow mangled. 5+
You're not into that mind reading bit againg now are you? :)
OriginalGriff 18-Apr-11 4:24am    
I tried fortune telling, but I could see no future in it...
durgeshtupkar10 18-Apr-11 5:34am    
But here, I've Four different textboxes.
Then how can I take them in one 'If Statement'?
durgeshtupkar10 18-Apr-11 6:01am    
actually i'm having 4 combo boxes not textboxes
It's too boring to organize a rat race between experts who can make the shortest code, but yes, you code is unreasonably long.

Remember, you goal is not to make the code shorted but to improve maintainability and readability. This goal would change the code in the same direction (making it shorter), but only to certain extent. Main principle here is D.R.Y. (Do not Repeat Yourself — http://en.wikipedia.org/wiki/Do_not_repeat_yourself[^]).

You do repeat. Look at Color.Red, same in all branches — carry it our of if blocks. Conditions are almost similar — find a common denominator. Create a predicate function checking the condition; the different part of condition will be a parameter: this is the Text of ether PackagingByName, ManufacturerName, ItemCategoryName or ItemTypeName. When you use a predicate, each condition will be one short line (predicate call with parameter). As to the predicates, you can try to use my trick — inner method in the anonymous forms; see my Tips/Trick article: Hide Ad-hoc Methods Inside the Calling Method’s Body[^] (some readers find my method to be not readable, but I think it is — you decide).

Another general advice: try to formulate the laces of logical checks informally and implement closer to this formulation.

—SA
 
Share this answer
 
Comments
Manfred Rudolf Bihy 18-Apr-11 4:22am    
Sage advice, SA! 5+
Sergey Alexandrovich Kryukov 18-Apr-11 4:29am    
Thank you, Manfred.
--SA
OriginalGriff 18-Apr-11 4:25am    
Good advice!
Sergey Alexandrovich Kryukov 18-Apr-11 4:28am    
Thank you, Griff.
--SA
Michel [mjbohn] 18-Apr-11 4:42am    
Good advice. My 5
I also prefer to give (and receive) general advice. In my opinion a general advice is way better for learning than a ready-to-go solution. It might be a misimpression, but I think that a general advice is often ignored or even downvoted by questioner.
One way for sure would be to factor out a function that does all this along the lines of

C#
public bool TestSetError(ComboBox control, String error)
{
    bool retVal = true;
    if (control.Text == string.Empty || control.Text == StringConstants.select ||
        control.SelectedValue.ToString() == StringConstants.select)
    {
        this.Master.LblError.Text= error;
        this.Master.LblError.ForeColor = Color.Red;
        control.Focus();
        retVal = false;
    }
 
}


return (TestSetError(ItemTypeName,     ErrorConstants.selectItemType) &&
        TestSetError(ItemCategoryName, ErrorConstants.selectItemCat) &&
        TestSetError(ManufacturerName, ErrorConstants.selectMake) &&
        TestSetError(PackagingByName,  ErrorConstants.selectPackage)
       );
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 18-Apr-11 4:13am    
???
Manfred Rudolf Bihy 18-Apr-11 4:16am    
Somehow my post got mangled. I'm reentering it right now! :)
Manfred Rudolf Bihy 18-Apr-11 4:19am    
Don't know what exactly happened, but after submitting my solution most of the code I had entered was gone.
Sergey Alexandrovich Kryukov 18-Apr-11 4:29am    
Looks not bad now, my 5.
--SA
Espen Harlinn 18-Apr-11 17:57pm    
My 5

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