Quick Watch returns wrong value
I have a very strange problem. I have this line of code:
Code:
Dim Phrase (1 to 300) as string
if Phrase(2) = Phrase(287) then ...
Now this doesn't work, even if Phrase(2) and Phrase(287) have exactly the same values ("Grade" in this case). When I break at this point and hover the mouse over Phrase(2) and Phrase(287), they both show "Grade". However, when I hit Shift + F9, Phrase(2) shows "Grade" but Phrase(287) shows "__. How can that be?
Re: Quick Watch returns wrong value
Could you try something this and tell us the result?
Code:
Msgbox Phrase(2),,Phrase(287)
if Phrase(2) = Phrase(287) then
Re: Quick Watch returns wrong value
Re: Quick Watch returns wrong value
No, sorry... It shows "Grade" and "Grade"
Re: Quick Watch returns wrong value
How about the title of the Msgbox? Nothing showed? If none then there is no value for Phrase(287).
Re: Quick Watch returns wrong value
Could you try this if it works then?
Code:
if CStr(Phrase(2)) = CStr(Phrase(287)) then
Re: Quick Watch returns wrong value
You may also try this.
Code:
If LCase$(Trim$(Phrase(2))) = LCase$(Trim$(Phrase(287))) Then
Re: Quick Watch returns wrong value
No, VB does not regard the two values as the same. Again CStr(Phrase(287)) gives a no value when using Shift&F9, but moving the mouse over it shows "Grade"
Re: Quick Watch returns wrong value
Re: Quick Watch returns wrong value
Yes; it also does not work.
I have other similar lines of code which does work, it seems as if it is only Phrase(287) which gives this problem...
Re: Quick Watch returns wrong value
How are you setting the value of Phrase(287)? There is also the StrComp function that you could try also.
Re: Quick Watch returns wrong value
This is my best bet, there could be a vbNullChar in your Phrase(287), I tested this and here's what I came up with.
Code:
Option Explicit
Private Sub Command1_Click()
Dim Phrase(1 To 300) As String
Dim a As Long
For a = LBound(Phrase) To UBound(Phrase)
Phrase(a) = "Grade"
Next
'will work
If Trim$(Phrase(2)) = Trim$(Phrase(287)) Then
MsgBox "x"
End If
Phrase(287) = Phrase(287) & vbNullChar
'will not work
If Trim$(Phrase(2)) = Trim$(Phrase(287)) Then
MsgBox "x"
End If
Phrase(287) = Replace$(Phrase(287), vbNullChar, vbNullString)
'will work
If Trim$(Phrase(2)) = Trim$(Phrase(287)) Then
MsgBox "x"
End If
End Sub
Re: Quick Watch returns wrong value
Thanks for the help; I'm going to try it tomorrow. It is now over midnight in my country...
Re: Quick Watch returns wrong value
You could also try to determine what consists the Phrase(287).
Code:
Private Sub Test()
ExtractCharacters Phrase(287)
End Sub
Private Sub ExtractCharacters(ByVal s As String)
Dim a As Long
For a = 1 To Len(s)
Debug.Print Asc(Mid$(s, a, 1))
Next
End Sub
Re: Quick Watch returns wrong value
Just before the if Phrase(2) = Phrase(287) then ... line you could try Debug.Print Len(Phrase(2)); Len(Phrase(287)). I'm betting that they are not the same length.
Re: Quick Watch returns wrong value
Yes, just before I read your post, I tried this and Len(Phrase(2)) = 5, but Len(Phrase(287)) = 6. However, Trim does not change the lengths.
Re: Quick Watch returns wrong value
I also tried the code that dee-u suggested and the first character of Phrase(287) is Chr(10). I think I understand now...
Re: Quick Watch returns wrong value
Re: Quick Watch returns wrong value
Try this then
Code:
Phrase(287) = Replace$(Phrase(287), Chr$(10), vbNullString)