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

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
 
A

Arne Vajhøj

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
 
R

Ratnesh Maurya

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
 
R

Ratnesh Maurya

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
 
N

not_a_commie

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.
 
N

not_a_commie

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.
 
P

Peter Morris

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.
 
P

Peter Morris

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.
 
R

Ratnesh Maurya

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
 
R

Ratnesh Maurya

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
 
R

Ratnesh Maurya

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.
 
R

Ratnesh Maurya

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.
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

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
 
B

Ben Voigt [C++ MVP]

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?
 
B

Ben Voigt [C++ MVP]

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top