Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why not True

Why not True

Scheduled Pinned Locked Moved C#
17 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    ziwez0
    wrote on last edited by
    #1

    // StatusID =2 // xGetStatusOID = 2 if (StatusID.CompareTo(xGetStatusOID).Equals(true)) { ii = 1; } else { ii = 2; } //Result //ii = 2

    S L P M A 5 Replies Last reply
    0
    • Z ziwez0

      // StatusID =2 // xGetStatusOID = 2 if (StatusID.CompareTo(xGetStatusOID).Equals(true)) { ii = 1; } else { ii = 2; } //Result //ii = 2

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      well, CompareTo returns an int, saying whether the passed argument is less, equals or greater than the object. You can rewrite your code like this:

      ii = (StatusID.Equals(xGetStatusOID) ? 1 : 2);

      That's all. Regards Sebastian

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      1 Reply Last reply
      0
      • Z ziwez0

        // StatusID =2 // xGetStatusOID = 2 if (StatusID.CompareTo(xGetStatusOID).Equals(true)) { ii = 1; } else { ii = 2; } //Result //ii = 2

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, what are you doing? did you check the return type of CompareTo? does your code even compile? why not simply if(StatusID==xGetStatusOID) { ... } else { ... }? I recommend you buy a book on the language or technology of interest and study it. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


        Z 1 Reply Last reply
        0
        • Z ziwez0

          // StatusID =2 // xGetStatusOID = 2 if (StatusID.CompareTo(xGetStatusOID).Equals(true)) { ii = 1; } else { ii = 2; } //Result //ii = 2

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Why use methods when operators will suffice?

          1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, what are you doing? did you check the return type of CompareTo? does your code even compile? why not simply if(StatusID==xGetStatusOID) { ... } else { ... }? I recommend you buy a book on the language or technology of interest and study it. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


            Z Offline
            Z Offline
            ziwez0
            wrote on last edited by
            #5

            This code does compile and returns false, perhaps you should of read what I wrote, it was a question.

            J L 2 Replies Last reply
            0
            • Z ziwez0

              This code does compile and returns false, perhaps you should of read what I wrote, it was a question.

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              ziwez0 wrote:

              perhaps you should of read what I wrote, it was a question.

              No you didnt, you just posted some code! Perhaps you should buy a beginner book on C# and work your way through it!

              Z 1 Reply Last reply
              0
              • Z ziwez0

                This code does compile and returns false, perhaps you should of read what I wrote, it was a question.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi, yes you are absolutely right, it does compile (well, after one adds type declarations for all the variables used), although it does not make sense whatsoever. CompareTo returns an int int.Equals(true) returns false no matter what. It may help to read some documentation[^] especially if you insist on using methods you don't really need and apparently don't understand well. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                modified on Friday, June 10, 2011 11:35 AM

                1 Reply Last reply
                0
                • Z ziwez0

                  // StatusID =2 // xGetStatusOID = 2 if (StatusID.CompareTo(xGetStatusOID).Equals(true)) { ii = 1; } else { ii = 2; } //Result //ii = 2

                  M Offline
                  M Offline
                  musefan
                  wrote on last edited by
                  #8

                  is it because : bool == 0; is false? you are comparing an 'integer' to a 'bool' which is always false as they are different types

                  My opinion is... If someone has already posted an answer, dont post the SAME answer

                  1 Reply Last reply
                  0
                  • Z ziwez0

                    // StatusID =2 // xGetStatusOID = 2 if (StatusID.CompareTo(xGetStatusOID).Equals(true)) { ii = 1; } else { ii = 2; } //Result //ii = 2

                    A Offline
                    A Offline
                    Alan N
                    wrote on last edited by
                    #9

                    Ok Lets break your code into steps int result1 = StatusID.CompareTo(xGetStatusOID); // result1 will be zero as the values are equal bool result2 = result1.Equals(true); //result2 will be false The overload of Equals your code would call is public override bool Equals (Object obj) and the documentation says The return value is true if obj is an instance of Int32 and equals the value of this instance; otherwise, false. Alan.

                    modified on Wednesday, February 4, 2009 12:01 PM

                    Z 1 Reply Last reply
                    0
                    • J J4amieC

                      ziwez0 wrote:

                      perhaps you should of read what I wrote, it was a question.

                      No you didnt, you just posted some code! Perhaps you should buy a beginner book on C# and work your way through it!

                      Z Offline
                      Z Offline
                      ziwez0
                      wrote on last edited by
                      #10

                      Hi j4amieC,

                      J4amieC wrote:

                      No you didnt, you just posted some code!

                      Suject "Why not True" http://www.thefreedictionary.com/why[^] I asked a question?, perhaps if you spent more time reading what people write, maybe your reponse might be helpful (like Alan's post), instead of your alternative which was a quick dismissal of someone's question.

                      1 Reply Last reply
                      0
                      • A Alan N

                        Ok Lets break your code into steps int result1 = StatusID.CompareTo(xGetStatusOID); // result1 will be zero as the values are equal bool result2 = result1.Equals(true); //result2 will be false The overload of Equals your code would call is public override bool Equals (Object obj) and the documentation says The return value is true if obj is an instance of Int32 and equals the value of this instance; otherwise, false. Alan.

                        modified on Wednesday, February 4, 2009 12:01 PM

                        Z Offline
                        Z Offline
                        ziwez0
                        wrote on last edited by
                        #11

                        Thanks Alan, I also looked at http://msdn.microsoft.com/en-us/library/fbh501kz(VS.80).aspx[^] I Was expecting it to return 1, but yes your right it will return 0

                        L 1 Reply Last reply
                        0
                        • Z ziwez0

                          Thanks Alan, I also looked at http://msdn.microsoft.com/en-us/library/fbh501kz(VS.80).aspx[^] I Was expecting it to return 1, but yes your right it will return 0

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          :confused: :confused: :confused: The page you provided a link to discusses string comparison. How does that relate to the original question? Further more whether CompareTo returns 0, 1 or any other number is irrelevant as long as you apply .Equals(true) to it, since that will ALWAYS return false.

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                          modified on Friday, June 10, 2011 11:35 AM

                          Z 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            :confused: :confused: :confused: The page you provided a link to discusses string comparison. How does that relate to the original question? Further more whether CompareTo returns 0, 1 or any other number is irrelevant as long as you apply .Equals(true) to it, since that will ALWAYS return false.

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                            modified on Friday, June 10, 2011 11:35 AM

                            Z Offline
                            Z Offline
                            ziwez0
                            wrote on last edited by
                            #13

                            Hi Luc, Yes the link was a string comparision, but dont forget that Int32 Supports the CompareToMethod, and they both return 0 if "strA equals strB." And as mentioned I thought it would return 1 and not 0.

                            L 1 Reply Last reply
                            0
                            • Z ziwez0

                              Hi Luc, Yes the link was a string comparision, but dont forget that Int32 Supports the CompareToMethod, and they both return 0 if "strA equals strB." And as mentioned I thought it would return 1 and not 0.

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #14

                              You read the wrong pages, you think you read the opposite of what they say, you ignore earlier good advice, in short you don't have a clue. I suggest a career change. :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                              modified on Friday, June 10, 2011 11:35 AM

                              Z 1 Reply Last reply
                              0
                              • L Luc Pattyn

                                You read the wrong pages, you think you read the opposite of what they say, you ignore earlier good advice, in short you don't have a clue. I suggest a career change. :)

                                Luc Pattyn [Forum Guidelines] [My Articles]


                                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                                modified on Friday, June 10, 2011 11:35 AM

                                Z Offline
                                Z Offline
                                ziwez0
                                wrote on last edited by
                                #15

                                Thanks for that, keep up the good advice.... :laugh:

                                L 1 Reply Last reply
                                0
                                • Z ziwez0

                                  Thanks for that, keep up the good advice.... :laugh:

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  You still don't understand that comparing 1 to true would still return false anyway. So it is irrelevant whether CompareTo would return 0, 1 or PI, the result would always be false in your case.

                                  Z 1 Reply Last reply
                                  0
                                  • L Lost User

                                    You still don't understand that comparing 1 to true would still return false anyway. So it is irrelevant whether CompareTo would return 0, 1 or PI, the result would always be false in your case.

                                    Z Offline
                                    Z Offline
                                    ziwez0
                                    wrote on last edited by
                                    #17

                                    Hi Greeeg, thanks for taking the time to post, yeah your right, I was just expecting it to return 1 (true), I was just curious, but after alans post made me look on MSDN, so it would be more like if (i.CompareTo(ii).Equals(0)) ((0\1)and not true\false) thanks tho.

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups