Groups | Blog | Home
all groups > vj# > september 2005 >

vj# : Testing for equality of strings in VJ#


Al Christoph
9/7/2005 5:27:02 PM
I am really puzzled in one piece of code the obvious worked and in another it
didn't

String s; // a global
.....
if (s == "") {
// blah blah blah
}
// the above works just fine
public void ...
String s; // local
if (s == "") {
// blah blah blah
}
// blah blah blah never happends even when quick watch shows the expression
is true.

I finally resorted to s.get_Length == 0 as the expression and it works fine.

What gives????

Side bar to MS lurkers. I'm appalled at the quality of the VS 2003 internal
doco for J#. Look up boolean expression in the index and all you get is
Crystal Repots. UGH!!!!! BTW one of my mottos is that "If Bill Gates had a
hand in it, you shouldn't need a manual." (The other is "Bill Gates won.")
--
Regards,
Al Christoph
Senior Consultant and Proprietor
Three Bears Software, LLC
David Anton
9/8/2005 7:42:07 AM
Use the CompareTo method on strings. Strings in J# are treated as true
objects, so the equality operator will compile, but provide reference
equality, not value equality.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code


[quoted text, click to view]
Bruno Jouhier
9/8/2005 9:38:28 PM
Remember: J# is some variant of Java. And Java specs say that == test the
references, not the values.

You can fix this by using equals:

if (s.equals("")) { ... }

Note: as J# gives you access to both the Java framework (JDK) and the .NET
framework, you have the choice between:
s.equals("") // JDK equals
s.Equals("") // .NET equals
These 2 methods are equivalent but this is not the case of all string
methods. So be careful. For example:
s1.compareTo(s2) // JDK version: compares the unicode values (binary
comparison)
s1.CompareTo(s2) // .NET version: compares according to current locale's
collation rules
and also
s1.indexOf(start, end) // JDK version: 2nd arg is end index
s1.IndexOf(start, len) // .NET version: 2nd arg is len of substring

So, I suggest that you learn some basics about the JDK and Java (like the
fact that == compares references) so that you don't get too many bad
surprises.

Bruno.

"Al Christoph" <AlChristoph@discussions.microsoft.com> a écrit dans le
message de news: C7623100-70FC-461F-8F3C-D0831D0545C2@microsoft.com...
[quoted text, click to view]

George Birbilis [MVP J#] [9880]
9/13/2005 12:00:00 AM
In the Java spec it's similar, never use == to compare strings, the
underlying objects will be compared for being the same object.

Sun Java VM happens to reuse string data (since strings are "immutable" [you
can't edit an existing string, but only make a new string or char array out
of it]), so that if you have two string fields holding the same text,
chances are they point to the same underlying "string" object. So sometimes
"==" will work in Sun Java VM, sometimes not (which can be quite tricky to
debug, but still saves some memory space esp. if you have quite big strings)

J# could do similarly, but still it doesn't solve your case, use ".equals"
method of a string class instance instead for comparison of two Java strings
(also the String class maybe has some static method to compare two given
strings)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
George Birbilis <birbilis@kagi.com>
Microsoft Most Valuable Professional
MVP J# for 2004, 2005
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ QuickTime (Delphi & ActiveX: VB, PowerPoint, .NET)
+ Plugs (InterProcess/Internet communication)
http://www.kagi.com/birbilis
+ Robotics
http://www.mech.upatras.gr/~robgroup
.........................................................................

[quoted text, click to view]


AddThis Social Bookmark Button