String comparison problem

G

Guest

I'm having a hard time comparing two strings - even if the strings are
identical, the If x <> y Then condition is always met. I'm going crazy.
Here's my code:

Dim x As String = "a"
Dim y As String = "a"
If x <> y Then
' it always goes here
End If

I also tried:
If Not (x = y) Then

and

Dim theSame As Boolean
theSame = x Like y
If Not(theSame) Then ' If theSame=False Then gives the same result
' always goes here
End If

with no success. Can you tell me what I'm doing wrong? Thank you very much.
 
M

Marina Levit [MVP]

This isn't possible, and just to make sure I'm not going insane, I ran your
code, and the If statement was not entered.

There is something else going on here, like the code isn't really getting
recompiled, or you aren't running the code you think you are, etc.
 
G

Guest

I ran the code in a debug mode, put a break on the If statement, and it's
being entered every single time. I have that code in my Validating event for
a text box. When I move the code somewhere else, such as Load event, it's
being evaluated properly. I don't understand why.
 
S

sloan

Did you try the string.Equals method

?


if not ( x.Equals(y)) then

end if

?


Eve said:
I'm having a hard time comparing two strings - even if the strings are
identical, the If x <> y Then condition is always met. I'm going crazy.
Here's my code:

Dim x As String = "a"
Dim y As String = "a"
If x <> y Then
' it always goes here
End If

I also tried:
If Not (x = y) Then

and

Dim theSame As Boolean
theSame = x Like y
If Not(theSame) Then ' If theSame=False Then gives the same result
' always goes here
End If

with no success. Can you tell me what I'm doing wrong? Thank you very
much.
 
P

Patrice

I would :
- add something new to make sure the code is not out of sync with the exe
file
- would check Length/Chars to perform my own comparison to make sure I
didn't entered some kind of non printable character when using the editor...
 
G

Guest

I tried that and it works. I did this:

if not ( x.Equals(y)) then
msgbox "test"
end if

then I did this:
if x<>y then
msgbox "test"
end if

and it also worked! I didn't have a msgbox before, so I couldn't really tell
if the comparison is working - I thought it's not because when I was stepping
through the code, it would go to the line inside the If stm. I was sure that
if the condition is not met, it'll go straight to End If stm. I don't
understand why when I'm stepping through the code using F8 or F10, it goes
inside the loop even if the condition is not met, but I'm glad it's working.
Thanks a lot for your help and quick responses!
 
J

Jon Skeet [C# MVP]

Eve said:
I ran the code in a debug mode, put a break on the If statement, and it's
being entered every single time. I have that code in my Validating event for
a text box. When I move the code somewhere else, such as Load event, it's
being evaluated properly. I don't understand why.

Well, the code you showed had nothing to do with a Load event, so we
clearly haven't got the full picture.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
M

Michael D. Ober

Do you have "Option Compare Binary" or "Option Compare Text" at the top of
your module. If it's binary, it may be that you are doing a case sensitive
compare.

Mike Ober.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top