Home All Groups Group Topic Archive Search About

String comparison problem

Author
12 Sep 2006 3:26 PM
Eve
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.

Author
12 Sep 2006 3:39 PM
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.

Show quote
"Eve" <E**@discussions.microsoft.com> wrote in message
news:DE9E1E41-DC57-4F4F-81D5-2E32F0804C19@microsoft.com...
> 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.
Author
12 Sep 2006 4:13 PM
Eve
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.

Show quote
"Marina Levit [MVP]" wrote:

> 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.
>
> "Eve" <E**@discussions.microsoft.com> wrote in message
> news:DE9E1E41-DC57-4F4F-81D5-2E32F0804C19@microsoft.com...
> > 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.
>
>
>
Author
12 Sep 2006 8:18 PM
Jon Skeet [C# MVP]
Eve <E**@discussions.microsoft.com> wrote:
> 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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
14 Sep 2006 3:36 AM
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.

Show quote
"Eve" <E**@discussions.microsoft.com> wrote in message
news:C8B63A83-CFBA-4E7D-B71F-D249AF2B89BD@microsoft.com...
> 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.
>
> "Marina Levit [MVP]" wrote:
>
> > 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.
> >
> > "Eve" <E**@discussions.microsoft.com> wrote in message
> > news:DE9E1E41-DC57-4F4F-81D5-2E32F0804C19@microsoft.com...
> > > 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.
> >
> >
> >
>
Author
12 Sep 2006 5:08 PM
sloan
Did you try the string.Equals method

?


if not ( x.Equals(y)) then

end if

?


Show quote
"Eve" <E**@discussions.microsoft.com> wrote in message
news:DE9E1E41-DC57-4F4F-81D5-2E32F0804C19@microsoft.com...
> 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.
Author
12 Sep 2006 5:37 PM
Eve
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!
Show quote
"sloan" wrote:

> Did you try the string.Equals method
>
> ?
>
>
> if not ( x.Equals(y)) then
>
> end if
>
> ?
>
>
> "Eve" <E**@discussions.microsoft.com> wrote in message
> news:DE9E1E41-DC57-4F4F-81D5-2E32F0804C19@microsoft.com...
> > 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.
>
>
>
Author
12 Sep 2006 5:18 PM
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...

--
Patrice

"Eve" <E**@discussions.microsoft.com> a écrit dans le message de news:
DE9E1E41-DC57-4F4F-81D5-2E32F0804***@microsoft.com...
Show quote
> 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.

AddThis Social Bookmark Button