Results 1 to 8 of 8

Thread: InStr

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    8

    InStr

    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

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Did you try this and gives you the same result ?

    VB Code:
    1. S = "[test]"
    2. If InStr(S, "[") And[B]Also[/B]  Instr(S, "]") then
    3. B = True
    4. Else
    5. B = False
    6. EndIf

  3. #3
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    take a look at the IndexOf property , it replaces the InStr of vb6 , eg:
    VB Code:
    1. Dim s As String = "[test]"
    2.         Dim b As Boolean = False
    3.         If Not s.IndexOf("[") = -1 AndAlso Not s.IndexOf("]") = -1 Then
    4.             b = True
    5.         Else
    6.             b = False
    7.         End If
    8.  
    9.         MessageBox.Show(b)
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    8
    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

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    InStr returns the position of the character or -1 if it's not in the haystack.

    in vb6

    VB Code:
    1. S = "[test]"
    2. If InStr(S, "[") And Instr(S, "]") then
    3. B = True
    4. Else
    5. B = False
    6. EndIf

    you'll find that B will always be false~

  6. #6
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Also

    Code:
    If Not s.IndexOf("[") = -1 AndAlso Not s.IndexOf("]") = -1 Then
    would work regardless of whether it's AndAlso or And

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    8
    I see it now.

    Thanks for setting me straight.

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Check MSDN Help for the differences between And and AndAlso .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width