Results 1 to 13 of 13

Thread: == -> .Equals(..) but what is != -> ??? [Resolved]

  1. #1

    Thread Starter
    Hyperactive Member onerrorgoto's Avatar
    Join Date
    Aug 1999
    Location
    Sweden
    Posts
    330

    Question == -> .Equals(..) but what is != -> ??? [Resolved]

    I want to do som comparisons.

    VB Code:
    1. in c++  :
    2. if(X=="1")  //(you can use it in C# to, I know)
    3. {
    4. //do something
    5. }
    6. c#.
    7. if(x.equals("1")
    8. {
    9. //do something
    10. }

    that is OK, but what about this.
    VB Code:
    1. in c++:
    2. if(X!="1")
    3. {
    4. //do something
    5. }
    6. c#.
    7. if(x.??("1")  //what to use here??
    8. {
    9. //do something
    10. }

    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

  2. #2
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    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;

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Hmm ,

    Code:
    if (!(x=="1"))   // not equal
    {
    //do something
    }

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Code:
    if(!X.Equals("1"))
       //do something

  5. #5
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    and just for fun are you doing a shallow comparison or a deep comparison?

  6. #6
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  7. #7
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    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. "

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  9. #9
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  10. #10
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    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.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  12. #12

    Thread Starter
    Hyperactive Member onerrorgoto's Avatar
    Join Date
    Aug 1999
    Location
    Sweden
    Posts
    330

    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:
    if(!X.Equals("1"))
    Thanks
    Onerrorgoto

    Dont be to optimistic, the light at the end of the tunnel might be a train

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width