C# Function

Hi,
I need to the following code to custom function.Any idea appreciate
 if (TxtPostcode.Text == null && string.IsNullOrWhiteSpace(TxtPostcode.Text))
                    {
                        validate = 1;
                    }

                    if (TxtDestinationName.Text == null && string.IsNullOrWhiteSpace(TxtDestinationName.Text))
                    {
                        validate = 1;
                    }

                    if (TxtDestinationDescription.Text == null && string.IsNullOrWhiteSpace(TxtDestinationDescription.Text))
                    {
                        validate = 1;
                    }

Open in new window

LVL 10
ukerandiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try:

private int validate(String value)
        {
            return value == null && string.IsNullOrWhiteSpace(value) ? 1 : 0; //or your default value;
        }

Open in new window


call it like:

int ValidatePostcode = validate(TxtPostcode.Text);
int ValidateDestinationName = validate(TxtDestinationName.Text);
int DestinationDescription = validate(TxtDestinationDescription.Text);

Open in new window

0
Pawan KumarDatabase ExpertCommented:
There is no need to check for null before checking isnullorwhitespace. We can directly use below -

private int validate(String value)
{
	return string.IsNullOrWhiteSpace(value) ? 1 : 0; //or your default value;
}

Open in new window

0
it_saigeDeveloperCommented:
And you can simplify it even further by doing a conversion of the boolean result; e.g. -
private int Validate(string value)
{
    return Convert.ToInt16(string.IsNullOrWhiteSpace(value));
}

Open in new window


-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Pawan KumarDatabase ExpertCommented:
@it_saige - You are returning int NOT boolean.
0
it_saigeDeveloperCommented:
@Pawan,

I did not say that I was returning a boolean.  The result of the IsNullOrWhiteSpace method is a boolean.

The preceeding examples were using a ternary operation to return a 1 or a 0 based upon that boolean result.  I am stating that those examples could be simplified further by converting the boolean result of IsNullOrWhiteSpace to an integer.

-saige-
0
Pawan KumarDatabase ExpertCommented:
Ohh that way .. cool :)
0
ukerandiAuthor Commented:
Hi Guy,Thanks i understand the logic in the Function but
issue is i can't use if condition for 4 or 5 time.
Any other better way to validate this
int ValidatePostcode = validate(TxtPostcode.Text);
int ValidateDestinationName = validate(TxtDestinationName.Text);
int DestinationDescription = validate(TxtDestinationDescription.Text);
IF(ValidatePostcode=0)
{

}


IF(ValidateDestinationName=0)
{

}


IF(DestinationDescription=0)
{

}
0
ukerandiAuthor Commented:
I have wrote a program using array to validate.It's working fine.
then for each() loop help me to validate.
thanks Guy
0
ukerandiAuthor Commented:
thax
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.