Why not True
-
// StatusID =2 // xGetStatusOID = 2 if (StatusID.CompareTo(xGetStatusOID).Equals(true)) { ii = 1; } else { ii = 2; } //Result //ii = 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.
-
// StatusID =2 // xGetStatusOID = 2 if (StatusID.CompareTo(xGetStatusOID).Equals(true)) { ii = 1; } else { ii = 2; } //Result //ii = 2
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
-
// StatusID =2 // xGetStatusOID = 2 if (StatusID.CompareTo(xGetStatusOID).Equals(true)) { ii = 1; } else { ii = 2; } //Result //ii = 2
Why use methods when operators will suffice?
-
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
-
This code does compile and returns false, perhaps you should of read what I wrote, it was a question.
-
This code does compile and returns false, perhaps you should of read what I wrote, it was a question.
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
-
// StatusID =2 // xGetStatusOID = 2 if (StatusID.CompareTo(xGetStatusOID).Equals(true)) { ii = 1; } else { ii = 2; } //Result //ii = 2
-
// StatusID =2 // xGetStatusOID = 2 if (StatusID.CompareTo(xGetStatusOID).Equals(true)) { ii = 1; } else { ii = 2; } //Result //ii = 2
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
-
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!
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.
-
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
-
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
: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
-
: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
-
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.
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
-
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
-
You still don't understand that comparing
1
totrue
would still returnfalse
anyway. So it is irrelevant whetherCompareTo
would return 0, 1 or PI, the result would always befalse
in your case.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.