|
-
Jul 4th, 2003, 07:08 AM
#1
Thread Starter
Hyperactive Member
== -> .Equals(..) but what is != -> ??? [Resolved]
I want to do som comparisons.
VB Code:
in c++ :
if(X=="1") //(you can use it in C# to, I know)
{
//do something
}
c#.
if(x.equals("1")
{
//do something
}
that is OK, but what about this.
VB Code:
in c++:
if(X!="1")
{
//do something
}
c#.
if(x.??("1") //what to use here??
{
//do something
}
Anyone?
Last edited by onerrorgoto; Jul 8th, 2003 at 02:53 AM.
Onerrorgoto
Dont be to optimistic, the light at the end of the tunnel might be a train
-
Jul 4th, 2003, 07:54 AM
#2
Lively Member
I'm not sure why you are using .equals? the following work in c# just like c++
char c = 'M';
if (c=='N')
do something;
if (c != 'N')
do something;
-
Jul 4th, 2003, 08:43 AM
#3
Sleep mode
Hmm ,
Code:
if (!(x=="1")) // not equal
{
//do something
}
-
Jul 4th, 2003, 08:47 AM
#4
Frenzied Member
Code:
if(!X.Equals("1"))
//do something
-
Jul 4th, 2003, 01:33 PM
#5
and just for fun are you doing a shallow comparison or a deep comparison?
-
Jul 6th, 2003, 08:37 AM
#6
yay gay
i think the .Equals() will do different results than the == because == makes a basic comparison while .Equals() uses a more approached way..maybe im saying poopie but i think thats the way it is..so for strings, ints etc you can use == but for complex types you should use .Equals()
\m/  \m/
-
Jul 6th, 2003, 08:51 AM
#7
Lively Member
You're right.
From: http://samples.gotdotnet.com/quickst...%2fequals.aspx
"To ask the computer to see if two different object references refer to the same object, use == (= or 'Is' in VB). This is why this scenario in the above sample resulted in False. In contrast, when you use the Equals method, you are asking if the objects have the same information in them, regardless of the actual objects they refer to. "
-
Jul 7th, 2003, 05:40 AM
#8
Which means you can't use == for strings - they are full-fledged objects.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 7th, 2003, 10:21 AM
#9
yay gay
hmm err but thats what we all do! i mean..i think about 99% of the guys who sees this forums use the == in string comp. lol and i never got any errors
\m/  \m/
-
Jul 7th, 2003, 10:29 AM
#10
Lively Member
Originally posted by CornedBee
Which means you can't use == for strings - they are full-fledged objects.
That would seem to be true, but it isn't. From the same article I refereced above:
"String objects are inherently 'immutable'. Although they act like objects in many ways, you can think of them as base data types. For example, say you make a string, MyName, and then make a new string, YourName, which you could set = to MyName. If you then changed YourName so it was different, MyName would not change. This is demonstrated in the following example. Base data types operate in the same way. "
And here is a test you can run to prove it:
string s1 = "test";
string s2 = "test";
Console.WriteLine(s1 == s2);
Console.WriteLine(s1.Equals(s2));
This prints True twice. If you change s2 to "test2", both lines print False.
-
Jul 7th, 2003, 02:49 PM
#11
Indeed. I thought you couldn't overload ==, but as of this:
For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.
User-defined value types can overload the == operator (see operator). So can user-defined reference types, although by default == behaves as described above for both predefined and user-defined reference types. If == is overloaded, != must also be overloaded.
apparently you can and MS did for String.
I'm used to not being able to in Java...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 8th, 2003, 02:11 AM
#12
Thread Starter
Hyperactive Member
Thanks
So basically it doesnt matter, I can use either == or .Equals(..)
But the question remains.
Is the an .NotEquals(..) or do I have to do as
DevGrp suggested:
Thanks
Onerrorgoto
Dont be to optimistic, the light at the end of the tunnel might be a train
-
Jul 8th, 2003, 02:26 AM
#13
There is no NotEquals. It would be implemented as !Equals anyway.
>So basically it doesnt matter, I can use either == or .Equals(..)
For types that have an overloaded == like string. For others it does matter.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|