If test question

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I am trying to find the most efficient way to run this.

If you have 4 variables: 2 zip codes and 2 cities. A person can supply a
zip code or not and a person can supply a city or not. I only want to test
the city if a city was passed and only want to pass a zip if zip is passed.
If both city and zip are passed both must passed.

Something like (I know this doesn't work):

string zip = "92780";
string zip2 = "92780";
string city = "";
string city2 = "tustin";

if ((zip != "" && (zip == zip2)) && (city != "" && (city ==
city2)))
do something

Where zip and city are the variable being passed and zip2 and city2 are
being returned.

In this case, if either zip or city are blank then the test fails.

So that if:

zip not blank and city blank - then only the zip = zip2 test counts and must
be true
city not blank and zip blank - then only the city == city2 test counts and
must be true
zip not blank and city not blank - then both zip == zip2 and city = city2
count and both must be true
zip blank and city blank - then the test fails.

Thanks,

Tom
 
I have it down to:

if (((zip != "" && city != "") && ((zip == zip2) && (city ==
city2))) ||
(city != "" && (zip == zip2)) ||
(zip != "" && (city == city2)))
do something

This should do something since the 2nd test says if city is blank (which it
is) and zip == zip2 (which it is - 92780). But it doesn't.

I have 3 tests here and only one has to be true - so why doesn't this work?

Thanks,

Tom
 
Found out why my example didn't work. The 2nd and 3rd tests need to be "=="
not "!="

if (((zip != "" && city != "") && ((zip == zip2) && (city ==
city2))) ||
(city == "" && (zip == zip2)) ||
(zip == "" && (city == city2)))
zip = zip;

Is there a better way to write this?

Thanks,

Tom
 
tshad said:
Found out why my example didn't work. The 2nd and 3rd tests need to
be "==" not "!="

if (((zip != "" && city != "") && ((zip == zip2) && (city
== city2))) || (city == "" && (zip == zip2)) ||
(zip == "" && (city == city2)))
zip = zip;

Is there a better way to write this?

What about:

if ((zip != "" && (zip == zip2)) || (city != "" && (city == city2)))
{
do something
}
 
Rudy Velthuis said:
What about:

if ((zip != "" && (zip == zip2)) || (city != "" && (city == city2)))
{
do something
}

That wouldn't handle the rule that says if both zip and city are not blank -
both tests must be true.

Thanks,

Tom
 
tshad said:
That wouldn't handle the rule that says if both zip and city are not
blank - both tests must be true.

Hmmm... that's true.

--
Rudy Velthuis http://rvelthuis.de

"Trying to get into the details seems to be a religious issue --
nearly everybody is convinced that every style but their own is
ugly and unreadable. Leave out the "but their own" and they're
probably right..." -- Jerry Coffin on indentation
 
tshad said:
I am trying to find the most efficient way to run this.

If you have 4 variables: 2 zip codes and 2 cities. A person can supply a
zip code or not and a person can supply a city or not. I only want to test
the city if a city was passed and only want to pass a zip if zip is passed.
If both city and zip are passed both must passed.
...
zip not blank and city blank - then only the zip = zip2 test counts and must
be true
city not blank and zip blank - then only the city == city2 test counts and
must be true
zip not blank and city not blank - then both zip == zip2 and city = city2
count and both must be true
zip blank and city blank - then the test fails.

I believe this will work:

if(
(zip == "" && city == "")
||
(zip != "" && zip != zip2)
||
(city != "" && city != city2)
)
{
// failure
}
 
Back
Top