473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

When "ToString" does not equal "ToString"

I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...c876a6acb.aspx
Jul 21 '05 #1
8 3140
Ayende Rahien wrote:
I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...34f-bfe0-39ac8
76a6acb.aspx


The problem is that "strings" are neither reference types nor value types!
They are handled in a special way...

In your case you are comparing object references which might be different!
Either use string.Compare(a, b) or a.CompareTo(b) or a.Equals(b)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jul 21 '05 #2
The code that is failing is the

if( o == test) ?

well o and test are both typed as object so you are using the System.Object == operator which hands off to System.Object.ReferenceEquals. In other words, the comparison will onyl succeed if o and test are teh same physical object

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<29*************************@posting.google.com>

I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...c876a6acb.aspx

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]
Jul 21 '05 #3
Umm, strings are reference types. They are immutable which results in some value like behavior but they are definitely reference types (i.e. allocated on the managed heap, referred to vai a 32 bit reference and are garbage collected.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<Xn**********************************@207.46.248.1 6>

Ayende Rahien wrote:
I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...34f-bfe0-39ac8
76a6acb.aspx


The problem is that "strings" are neither reference types nor value types!
They are handled in a special way...

In your case you are comparing object references which might be different!
Either use string.Compare(a, b) or a.CompareTo(b) or a.Equals(b)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]
Jul 21 '05 #4
Jochen Kalmbach <no********************@holzma.de> wrote:
Ayende Rahien wrote:
I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...34f-bfe0-39ac8
76a6acb.aspx


The problem is that "strings" are neither reference types nor value types!
They are handled in a special way...


That's not true. String is a reference type which overrides operator==.

In what way do you believe they're not reference types?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5
Jon Skeet [C# MVP] wrote:
> http://www.ayende.com/Blog/PermaLink...0-434f-bfe0-39
> ac8 76a6acb.aspx


The problem is that "strings" are neither reference types nor value
types! They are handled in a special way...


That's not true. String is a reference type which overrides
operator==.

In what way do you believe they're not reference types?


As Richard explains:
They are immutable which results in some value like behavior but they are
definitely reference types.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jul 21 '05 #6
Jochen Kalmbach <no********************@holzma.de> wrote:
The problem is that "strings" are neither reference types nor value
types! They are handled in a special way...


That's not true. String is a reference type which overrides
operator==.

In what way do you believe they're not reference types?


As Richard explains:
They are immutable which results in some value like behavior but they are
definitely reference types.


Right. There's nothing particularly special about string being
immutable though - it's perfectly easy to write your own immutable
reference types, and it's often a good idea.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #7
Even I had the same problem when I try to compare two strings one from
DataTable and a value in string. Then I found out that one string
contains some extra spaces at the end. So I used Trim and then it
worked. I am nnot sure about your problem. but just for information.

Jul 21 '05 #8
Have you tried
if(o.Equals(test))

"Ayende Rahien" <ay****@gmail.com> wrote in message
news:29*************************@posting.google.co m...
I've a really strange problem, in some part of my code I compare two
strings (through object), and while I *know* that they equal each
other, and in the watch window they do equal each other, then the
comparision return false.

See here for details:

http://www.ayende.com/Blog/PermaLink...c876a6acb.aspx
Nov 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ayende Rahien | last post by:
I've a really strange problem, in some part of my code I compare two strings (through object), and while I *know* that they equal each other, and in the watch window they do equal each other, then...
1
by: Steve Norman | last post by:
Hi All, I am trying to display an int as it' sHex representation, and looking on MSDN it appears this can be done with .ToString("X"). This does work, but the example on MSDN...
5
by: Dan C Douglas | last post by:
I have just installed VS.NET 2003 on my computer. I have a project that I have been developing on VS.NET 2002. I haven't upgraded this project to VS.NET 2003 yet and I am still developing it in...
0
by: Norman Yuan | last post by:
I posted this issue a while ago. After moveing the ASP.NET 1.1 app to a brand new server, the same problem still there. System: Winows2003 server std. .NET 1.1 App: Invoicing system Problem:...
4
by: Larry Smith | last post by:
Once you call "ToString()" for an object, how can you then convert the string back to the original object (assuming you know its type of course)? Also, why does "ToString()" produce one string and...
4
by: Greg Collins [Microsoft MVP] | last post by:
Will DateTime.Now.ToString("s") ALWAYS return the same formatting (i.e. the sortable XML Schema style formatting) regardless of what culture the operating system is set to? I've done limited...
3
by: Senna | last post by:
Hi When you have something like this: decimal d = 17.4m; Response.Write(d.ToString("c")); The output becomes: 17,50 kr. If the culture is set to swedish. So my question is. Where in the...
6
by: Zeng | last post by:
Math.Round has good behavior as following: Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is even Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.