Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Visual J# / September 2005

Tip: Looking for answers? Try searching our database.

Testing for equality of strings in VJ#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Al Christoph - 08 Sep 2005 01:27 GMT
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.")
Signature

Regards,
Al Christoph
Senior Consultant and Proprietor
Three Bears Software, LLC
just right software @ just right prices  @3bears.biz

David Anton - 08 Sep 2005 15:42 GMT
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.
Signature

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

> I am really puzzled in one piece of code the obvious worked and in another it
> didn't
[quoted text clipped - 21 lines]
> 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.")
George Birbilis [MVP J#] [9880] - 13 Sep 2005 12:22 GMT
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
........................................................................

> Use the CompareTo method on strings.  Strings in J# are treated as true
> objects, so the equality operator will compile, but provide reference
[quoted text clipped - 25 lines]
> > 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.")
Bruno Jouhier - 08 Sep 2005 20:38 GMT
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.

>I am really puzzled in one piece of code the obvious worked and in another
>it
[quoted text clipped - 25 lines]
> 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.")

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.