Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String1 = String2 fails even though the strings are the same.

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
I can't figure out what Microsoft insanity is going on here. I'm trying to keep track of what screen the form is on when the program is closed. When it opens back up it will start on the My.Settings.LastScreen. I decided the easiest way was simply track the device names. I iterate through the screens and look for the matching name.

Code:
Dim CurrentScreen As Screen
Dim Screens As Screen() = Screen.AllScreens

For Each cScreen As String In Screens
If My.Settings.LastScreen = cScreen.DeviceName Then CurrentScreen = cScreen
Next

Very simple. However it doesn't work. Even when the two values are the same they are never the same. No matter what I do to them .ToString/.ToLower/.Trim/etc they never equal even though when I break and look at the values they are the same ("\\.\DISPLAY1 or "\\.\DISPLAY1"). WT?


-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorry, Yes. In my code it is correct. There is more to the actual code so it was just easier to type up the part that was the acutal problem.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Is it possible that there are some non-visible characters within the string? For example, if there is a tab character (or carriage return, line feed, etc....) it may be difficult to see. I would suggest that you temporarily write some code to display the ASCII code of each character in the string.

Clearly, something must be different about them. Once you determine what it is, it shouldn't be too difficult to correct this problem.


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I'll try that, but I click on the string to expand it in the debugger and there is nothing as those do show up in there if the string contains them.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Nope, they are the same. Thanks for the thought though.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
You're not going to like this ... I certainly don't; but if you assign My.Settings.LastScreen to a String variable and then add a watch, you'll see that the actual settings value is enclosed in double quotes, even though that isn't displayed in the Text Visualizer.
 
As far as I remember all strings do in the watch to show they are strings. So does cScreen if you set it to a string. One thing I did fail to mention yesterday is if you break them out into characters some how there are extra character that do not show, however those character do exist in LastScreen as well. I was so frustrated and had a headache I didn't even think to mention it.

It has to be something about those that is throwing it off even though they exist in both. One thing I've done for now is set the char ascii value to a string and check that way. If I check them a char arrays they do not match either, but if i convert it to a string of ascii then it will read them as a match.
929246926873838076658949000063636363636363630063631063 =
929246926873838076658949000063636363636363630063631063

DaveInIowa did you try seeing if they were equal and does it give you the same problem?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Just a thought..... Could one of these strings be Unicode instead of ASCII?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Probably won't work, but have you tried

Code:
If My.Settings.LastScreen.Equals(cScreen.DeviceName)
 
Yes, I am seeing the same problem.

Possibly the problem lies in the single double-quote character at the beginning of cScreen.DeviceName. Since Settings are stored in XML, I think this single quote is making the XML malformed. I received an exception when trying to set My.Settings.LastScreen equal to cScreen.DeviceName. The exception was "Invalid high surrogate character". Maybe you could change the first character before and after saving it to My.Settings.
 
Thanks for the help everyone.

@gmmastros
As far as I know technically both should actually be Unicode as that is how vb.net stores a string in memory. Still I can't explain how/why the string is coming out 31 characters long, but both are coming out that way.

@RiverGuy
Tried real quick and nope.

@DaveInIowa
I don't get that error as that was how I got the last screen in there was to set My.Settings.LastScreen equal to cScreen.DeviceName whenever the program closes. I tried real quick and didn't work. I have My.Settings.LastScreen set as a string.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I have it on good authority that this should work:

Code:
Dim CurrentScreen As Screen
Dim Screens As Screen() = Screen.AllScreens

For Each cScreen As Screen In Screens
  My.Settings.LastScreen = cScreen.DeviceName
  My.Settings.Save()
  If My.Settings.LastScreen = cScreen.DeviceName Then
    CurrentScreen = cScreen
  End If
Next

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Just a thought, but have you tried converting both strings to lower case while doing the compare? I had a problem with this in one of my last apps and it took forever for me to find.

It should look something like this:
If My.Settings.LastScreen.ToLower = cScreen.DeviceName.ToLower Then
CurrentScreen = cScreen
End If

(txmed)
 
Sorry, first time to get back to this since.

@gmmastros
It will work fine when you first check it, but once you pull it again from a string later then they do not match.

@txmed
Nice thought, but nope.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top