how can I use the OR operator or is there another option better thanthis...

  • Thread starter Thread starter trint
  • Start date Start date
T

trint

I need to skip an operation if these conditions are as follows:

if(string1 != string2) or (string1 != string3) or (string1 != string4)
{
do this...
}

I may just have drawn a blank, but your help is appreciated.
Thanks,
Trint
 
trint said:
I need to skip an operation if these conditions are as follows:

if(string1 != string2) or (string1 != string3) or (string1 != string4)
{
do this...
}

I may just have drawn a blank, but your help is appreciated.

if((string1 != string2) || (string1 != string3) || (string1 != string4))

should be fine.

Arne
 
Thanks.  I'll try that.
Trint

Hi Trint,

Better to use if (!string1.equals(string2)) than if(string1!=string2),
if you are checking contents to be equal
you need to take care of null values too.

Cheers,
-Ratnesh
S7 Software
 
Thanks.  I'll try that.
Trint

Hi Trint,

Better to use if (!string1.equals(string2)) than if(string1!=string2),
if you are checking contents to be equal
you need to take care of null values too.

Cheers,
-Ratnesh
S7 Software
 
Better to use if (!string1.equals(string2)) than if(string1!=string2),
if you are checking contents to be equal
you need to take care of null values too.

I'm pretty sure the string class overloads the '!=' and '==' to
compare references and then contents if the reference comparison
fails. Is that not the case? You have to beware of nulls for what you
are suggesting.
 
Better to use if (!string1.equals(string2)) than if(string1!=string2),
if you are checking contents to be equal
you need to take care of null values too.

I'm pretty sure the string class overloads the '!=' and '==' to
compare references and then contents if the reference comparison
fails. Is that not the case? You have to beware of nulls for what you
are suggesting.
 
Better to use if (!string1.equals(string2)) than if(string1!=string2),
if you are checking contents to be equal
you need to take care of null values too.
<<

This approach is likely to suffer from null reference exceptions, whereas
the original != one isn't.
 
Better to use if (!string1.equals(string2)) than if(string1!=string2),
if you are checking contents to be equal
you need to take care of null values too.
<<

This approach is likely to suffer from null reference exceptions, whereas
the original != one isn't.
 
I'm pretty sure the string class overloads the '!=' and '==' to
compare references and then contents if the reference comparison

No it doesnt, it works because of string "intern pool" the .Net
provides for optimization.
So if you have two string literals containing same string value, then
most probably both of them references to same string literal in intern
pool.
But it is never safe.
fails. Is that not the case? You have to beware of nulls for what you
are suggesting.

Yes that is the pain you need to take for good. :)

Cheers,
-Ratnesh
 
I'm pretty sure the string class overloads the '!=' and '==' to
compare references and then contents if the reference comparison

No it doesnt, it works because of string "intern pool" the .Net
provides for optimization.
So if you have two string literals containing same string value, then
most probably both of them references to same string literal in intern
pool.
But it is never safe.
fails. Is that not the case? You have to beware of nulls for what you
are suggesting.

Yes that is the pain you need to take for good. :)

Cheers,
-Ratnesh
 
No it doesnt, it works because of string "intern pool" the .Net
provides for optimization.
So if you have two string literals containing same string value, then
most probably both of them references to same string literal in intern
pool.
But it is never safe.


Yes that is the pain you need to take for good. :)

Cheers,
-Ratnesh

Oops I was wrong string class does overloads "==" and "!=" operators,
sorry for wrong info.
 
No it doesnt, it works because of string "intern pool" the .Net
provides for optimization.
So if you have two string literals containing same string value, then
most probably both of them references to same string literal in intern
pool.
But it is never safe.


Yes that is the pain you need to take for good. :)

Cheers,
-Ratnesh

Oops I was wrong string class does overloads "==" and "!=" operators,
sorry for wrong info.
 
Ratnesh said:
No it doesnt, it works because of string "intern pool" the .Net
provides for optimization.
So if you have two string literals containing same string value, then
most probably both of them references to same string literal in intern
pool.
But it is never safe.

C# != Java

Arne
 
Ratnesh said:
No it doesnt, it works because of string "intern pool" the .Net
provides for optimization.
So if you have two string literals containing same string value, then
most probably both of them references to same string literal in intern
pool.
But it is never safe.

C# != Java

Arne
 
trint said:
I need to skip an operation if these conditions are as follows:

if(string1 != string2) or (string1 != string3) or (string1 != string4)
{
do this...
}

I may just have drawn a blank, but your help is appreciated.
Thanks,
Trint

Unless string2, string3, and string4 are the same, your test is always true.
Are string2, string3, and string4 literals or variables?
 
trint said:
I need to skip an operation if these conditions are as follows:

if(string1 != string2) or (string1 != string3) or (string1 != string4)
{
do this...
}

I may just have drawn a blank, but your help is appreciated.
Thanks,
Trint

Unless string2, string3, and string4 are the same, your test is always true.
Are string2, string3, and string4 literals or variables?
 
Back
Top