Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is statement not working

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
0
0
US
Hi
I have this if statement:

Code:
if ((MinLives.ToUpper() != "ALL") || (MaxLives.ToUpper() != "ALL"))
{
	try
	{
	if (Convert.ToInt32(MinLives) < Convert.ToInt32(MaxLives))
		{
		sb.Append(" and ci.LIVE_LIVES between " + MinLives + "  and  " + MaxLives);
		}
	else
		{
		sb.Append(" and ci.LIVE_LIVES between " + MaxLives + "  and  " + MinLives );
		}
	}
	catch (System.FormatException ex)
	{
		throw new Exception("Please select two numeric values");
	}
}

What I want is, if either of the fields is "ALL" do not execute the try/catch block, but when I test, I pass in MinLives = ALL and MaxLives = 2000 and It still tries to execute the try/catch block. What am I doing wrong?

Thanks.
 
It is executing the block because MaxLives is satisfying the condition you specified. It isn't equal to All, therefore it is executing the try/catch.

I think you need && not ||.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
If i use && won't they both need to be All. I am trying to avoid someine passing in All and a number, by mistake? So im thing that passing && will say if MinLives is not "ALL" and MaxLives is not "ALL" execute the code. That my understanding of using &&, both need to be true. Am I wrong?

Thanks
 
if you use &&, neither of them can be "ALL"

if MinLives is not equal to "ALL" and (&&)
MaxLives is not equal to "ALL" then
convert them both to Integers.
 
Yes, what JurkMonkey said is correct. You said that if either of the values was equal to "ALL" to not execute the statement.

Your condition is ((MinLives.ToUpper() != "ALL") || (MaxLives.ToUpper() != "ALL"))

This way, if either one isn't "ALL", it goes in.

You need && so that both ARE NOT "ALL" to enter. Using && here, if either are ALL, it will compute to false and skip executing the try/catch.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
OR (pun intended)
if (!((MinLives.ToUpper() == "ALL") || (MaxLives.ToUpper() == "ALL")))
Marty
 
That would be a double negative of sorts.

It can make your code confusing.

The best time to use that is when you want to switch a boolean value such as:

bool allowchange = false;

allowchange = !allowchange //Sets to true if false, false if true
 
No argument JM, The post was cracking me up.
I still draw truth tables.
Marty
 
Your condition "What I want is, if either of the fields is "ALL" do not execute the try/catch block"

converts itself into the followinfg logic:

Code:
if ((MinLives.ToUpper() == "ALL") || (MaxLives.ToUpper() == "ALL"))
{
   ; //do nothing
}
else
{
   try
   {
      ....
   }
}

So, I do not get what for you ahve used the "!=" comparison. Anyway, in order to avoid use the else, you may use a "!" in front of the whole if expresion...

HTH,

s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top