S = "[test]"
If InStr(S, "[") And Instr(S, "]") then
B = True
Else
B = False
EndIf
In VB6 B = True, in .Net B = False
Can someone explain why?
Thanks,
Robb
Printable View
S = "[test]"
If InStr(S, "[") And Instr(S, "]") then
B = True
Else
B = False
EndIf
In VB6 B = True, in .Net B = False
Can someone explain why?
Thanks,
Robb
Did you try this and gives you the same result ?
VB Code:
S = "[test]" If InStr(S, "[") And[B]Also[/B] Instr(S, "]") then B = True Else B = False EndIf
take a look at the IndexOf property , it replaces the InStr of vb6 , eg:
VB Code:
Dim s As String = "[test]" Dim b As Boolean = False If Not s.IndexOf("[") = -1 AndAlso Not s.IndexOf("]") = -1 Then b = True Else b = False End If MessageBox.Show(b)
AndAlso makes it work.
I also found that:
If InStr(S, "[") > 0 And Instr(S, "]") > 0 then
works as well.
I didn't try the Not Index of -1 version. I am assuming it will work also because of the AndAlso instead of the And.
So can anyone tell me why the original works in VB but to get the same code to work in .net I have to explicitly say 'is greater than 0' or change the wording from And to AndAlso ?
The reason may become important in other scenarios down the road.
Thanks,
Robb
InStr returns the position of the character or -1 if it's not in the haystack.
in vb6
VB Code:
S = "[test]" If InStr(S, "[") And Instr(S, "]") then B = True Else B = False EndIf
you'll find that B will always be false~
Also
would work regardless of whether it's AndAlso or AndCode:If Not s.IndexOf("[") = -1 AndAlso Not s.IndexOf("]") = -1 Then
I see it now. :blush:
Thanks for setting me straight.:)
Check MSDN Help for the differences between And and AndAlso .