Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
if (string.IsNullOrEmpty(TxtUsername.Text))
                    {
                        LblMessage.Text = "Enter Username";
                    }
                    else
                    {
                        if (TxtUsername.Text.Length < 5)
                        {
                            LblMessage.Text = "Username Should be Minimum 5 character";
                        }
                        else
                        {
                            if (!validateStr(mRegExUsername, TxtUsername.Text))
                            {
                                LblMessage.Text = "Check Note below for guidelines.";
                            }
                            else
                            {
                                if (RDBtnSignup.Checked && BLLLoginDetails.Instance.CheckUsernameAvailability(TxtUsername.Text) != 1)
                                {
                                    LblMessage.Text = CISMessages.NotAvailableMsg;
                                }
                                else
                                {
                                    secemail.Visible = !RDIsOwner.Checked;
                                    e.Cancel = false;
                                }
                            }
                        }
                    }
Posted
Comments
__TR__ 24-Dec-12 8:54am    
Your question is not clear. Use "Improve Question" widget to provide more information on what is it you need help with.
snorkie 24-Dec-12 8:58am    
change the font :) Seriously, please help us understand the context of the problem so the correct solution can be offered.
bbirajdar 24-Dec-12 9:02am    
delete it...

C#
if (string.IsNullOrEmpty(TxtUsername.Text))
{
    LblMessage.Text = "Enter Username";
}
else if (TxtUsername.Text.Length < 5)
{
    LblMessage.Text = "Username Should be Minimum 5 character";
}
else if (!validateStr(mRegExUsername, TxtUsername.Text))
{
    LblMessage.Text = "Check Note below for guidelines.";
}
else if (RDBtnSignup.Checked && BLLLoginDetails.Instance.CheckUsernameAvailability(TxtUsername.Text) != 1)
{
    LblMessage.Text = CISMessages.NotAvailableMsg;
}
else
{
    secemail.Visible = !RDIsOwner.Checked;
    e.Cancel = false;
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Dec-12 12:45pm    
This text can be reduced by 11 lines at most, 8 at least. Most likely, e.Cancel is always false at the moment of assignment, so you can remove this line. And then you remove 10 lines with '{', '}'. If Cancel line is important, you remove 8 bracket lines.
—SA
Because you need each test to do something different, there really isn't a lot you can reduce there, except to change the formatting slightly:
C#
if (string.IsNullOrEmpty(TxtUsername.Text))
    {
    LblMessage.Text = "Enter Username";
    }
else if (TxtUsername.Text.Length < 5)
    {
    LblMessage.Text = "Username Should be Minimum 5 character";
    }
else if (!validateStr(mRegExUsername, TxtUsername.Text))
    {
    LblMessage.Text = "Check Note below for guidelines.";
    }
else if (RDBtnSignup.Checked && BLLLoginDetails.Instance.CheckUsernameAvailability(TxtUsername.Text) != 1)
    {
    LblMessage.Text = CISMessages.NotAvailableMsg;
    }
else
    {
    secemail.Visible = !RDIsOwner.Checked;
    e.Cancel = false;
    }
Which is slightly clearer to read.
 
Share this answer
 
First, you can remove most of the brackets:
C#
if (string.IsNullOrEmpty(TxtUsername.Text))
    LblMessage.Text = "Enter Username";
else if (TxtUsername.Text.Length < 5)
    LblMessage.Text = "Username Should be Minimum 5 character";
else if (!validateStr(mRegExUsername, TxtUsername.Text))
    LblMessage.Text = "Check Note below for guidelines.";
else if (RDBtnSignup.Checked && BLLLoginDetails.Instance.CheckUsernameAvailability(TxtUsername.Text) != 1)
    LblMessage.Text = CISMessages.NotAvailableMsg;
else
{
    secemail.Visible = !RDIsOwner.Checked;
    e.Cancel = false;
}

But then, it's most likely that this is a body of some event handler and e.Cancel was not assigned in this handler before. If so, it is already null, so you can remove more three lines:
C#
if (string.IsNullOrEmpty(TxtUsername.Text))
    LblMessage.Text = "Enter Username";
else if (TxtUsername.Text.Length < 5)
    LblMessage.Text = "Username Should be Minimum 5 character";
else if (!validateStr(mRegExUsername, TxtUsername.Text))
    LblMessage.Text = "Check Note below for guidelines.";
else if (RDBtnSignup.Checked && BLLLoginDetails.Instance.CheckUsernameAvailability(TxtUsername.Text) != 1)
    LblMessage.Text = CISMessages.NotAvailableMsg;
else
    secemail.Visible = !RDIsOwner.Checked;

But I doubt that it all makes sense, because the code does not look properly designed. We just did not touch your logic, and this is what should really be improved. To improve it more, some information on what do you want to achieve would be needed.

—SA
 
Share this answer
 
Comments
__TR__ 24-Dec-12 13:59pm    
+5
Sergey Alexandrovich Kryukov 24-Dec-12 14:06pm    
Thank you,
—SA
You can create a function like:

C#
protected string validateData(string _userName)
{
if(_userName == "")
return "Enter Username";
if(another failed condition)
return "Another error message";
.
.
.
.
return "Data Validated";
}


Then check like this:

C#
string _result = validateData(TxtUsername.Text);

if(_result == "Data Validated")
{
proceed with your functionality;
}
else
{
LblMessage.Text = _result;
}



This approach will not only make your code looks clean but also in case of any failed condition, the control will return the error rather than moving forward and check all the remaininng conditions. Just modify the function with your parameters and try.
 
Share this answer
 

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