Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. instr function not accepting arguments VB.net

instr function not accepting arguments VB.net

Scheduled Pinned Locked Moved Visual Basic
csharpregexhelpquestion
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xman125
    wrote on last edited by
    #1

    Cannot get instr function to work .net? Using VB.net For some reason, the 'instr' function and the 'string.indexof' is the same. The problem is that one of the 2 string arguments, is a variable that is passed from the calling subroutine (not the literal characters) and it does not recognise the 'match'. When I type in the literal characters with " " -- it works ok. Here is a copy of the line that is not working. 'm' is the positional marker / 'largetxt' the body that is being searched and 'smalltxt' is the string that is being searched for. intx = InStr(m, mainTxt, smallTxt) The variable 'smallTxt' is the problem ---- if I have: intx = InStr(m, mainTxt, "house") --- it works ok Also ... same thing for: intx = largeTxt.IndexOf(smallTxt, m) Cheers

    D Z M 3 Replies Last reply
    0
    • X xman125

      Cannot get instr function to work .net? Using VB.net For some reason, the 'instr' function and the 'string.indexof' is the same. The problem is that one of the 2 string arguments, is a variable that is passed from the calling subroutine (not the literal characters) and it does not recognise the 'match'. When I type in the literal characters with " " -- it works ok. Here is a copy of the line that is not working. 'm' is the positional marker / 'largetxt' the body that is being searched and 'smalltxt' is the string that is being searched for. intx = InStr(m, mainTxt, smallTxt) The variable 'smallTxt' is the problem ---- if I have: intx = InStr(m, mainTxt, "house") --- it works ok Also ... same thing for: intx = largeTxt.IndexOf(smallTxt, m) Cheers

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      InStr is only there for converted VB6 code. It's been deprecated. Use _string_.IndexOf() instead. Without seeing the contents of the variables, it's impossible to tell you why it isn't working with any confidence. These functions are case-sensitive, so that might be your problem, but...

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      1 Reply Last reply
      0
      • X xman125

        Cannot get instr function to work .net? Using VB.net For some reason, the 'instr' function and the 'string.indexof' is the same. The problem is that one of the 2 string arguments, is a variable that is passed from the calling subroutine (not the literal characters) and it does not recognise the 'match'. When I type in the literal characters with " " -- it works ok. Here is a copy of the line that is not working. 'm' is the positional marker / 'largetxt' the body that is being searched and 'smalltxt' is the string that is being searched for. intx = InStr(m, mainTxt, smallTxt) The variable 'smallTxt' is the problem ---- if I have: intx = InStr(m, mainTxt, "house") --- it works ok Also ... same thing for: intx = largeTxt.IndexOf(smallTxt, m) Cheers

        Z Offline
        Z Offline
        Zaf Khan
        wrote on last edited by
        #3

        Here is a sample for you...

        Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim maintext As String
            Dim smalltext As String
            Dim foundpos As Integer
            Dim startpos As Integer
            'startpos = 6     'should test not found
            startpos = 0      'should test matched
            maintext = "Jack and Jill"
            smalltext = "and"
            foundpos = maintext.IndexOf(smalltext, startpos)
            If foundpos > 0 Then
                'found
                MsgBox("match found at character " & foundpos)
            Else
                'not found
                MsgBox("no match found")
            End If
        
        End Sub
        
        X 1 Reply Last reply
        0
        • Z Zaf Khan

          Here is a sample for you...

          Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              Dim maintext As String
              Dim smalltext As String
              Dim foundpos As Integer
              Dim startpos As Integer
              'startpos = 6     'should test not found
              startpos = 0      'should test matched
              maintext = "Jack and Jill"
              smalltext = "and"
              foundpos = maintext.IndexOf(smalltext, startpos)
              If foundpos > 0 Then
                  'found
                  MsgBox("match found at character " & foundpos)
              Else
                  'not found
                  MsgBox("no match found")
              End If
          
          End Sub
          
          X Offline
          X Offline
          xman125
          wrote on last edited by
          #4

          Thank you both . It the 'indexof' function also not working .... it gives a true result may one time in 20 (when I know it is a true hit). I think there may be a bug in vb.net. It doesn't matter what I do, it comes up with -1 all the time.

          X Z D 3 Replies Last reply
          0
          • X xman125

            Thank you both . It the 'indexof' function also not working .... it gives a true result may one time in 20 (when I know it is a true hit). I think there may be a bug in vb.net. It doesn't matter what I do, it comes up with -1 all the time.

            X Offline
            X Offline
            xman125
            wrote on last edited by
            #5

            Is there an alternative method ?.... .... I could spend 5 years tyring to get this one working - it's driving me crazy.

            Z L 2 Replies Last reply
            0
            • X xman125

              Thank you both . It the 'indexof' function also not working .... it gives a true result may one time in 20 (when I know it is a true hit). I think there may be a bug in vb.net. It doesn't matter what I do, it comes up with -1 all the time.

              Z Offline
              Z Offline
              Zaf Khan
              wrote on last edited by
              #6

              Did you try the code portion i added in my thread? I tested it and it works fine... obviously we can see the word AND is in the string JACK AND JILL..... It makes me wonder what the ACTUAL strings are that you are comparing. Maybe you could add them in a thread? Its highly unlikely that the IndexOf method/function isnot working! Its quite possible though that your installation of the development environment may have a corrupted file and possibly the next step would be to uninstall and re-install it again. Still though, maybe you could save some time by adding your strings which your using in the comparision or even to upload the ACTUAL code block which is causing the offence? Also, if your searching for a single character without specifying the number of consecutive characters to search, then the character must be in UNICODE, maybe that is of relavance? Finally IndexOf is Zero based, so the FIRST character is position 0 then increases upwards, meaning it does not start with 1 but with zero. Looking at the string "JACK AND JILL" and searching for a match of "AND" the code block I gave you states a match is found at charcter position 5, which taking it from a zero based index is ACTUALLY chracter 6 when starting from 1... So maybe as a last resort you could subtract one from your start point (or start at zero anyway) and look for a match you KNOW exists in your string.

              1 Reply Last reply
              0
              • X xman125

                Is there an alternative method ?.... .... I could spend 5 years tyring to get this one working - it's driving me crazy.

                Z Offline
                Z Offline
                Zaf Khan
                wrote on last edited by
                #7

                Yes theres INSTR,

                If INSTR(Look-in-this-string", "For-This-String") > 0 Then
                'REM A Match Was Found
                Else
                'REM No_Match_Was_Found
                End If

                and if you assign the result to a variable then the variable will give you the position...

                LocationId = INSTR(Look-in-this-string", "For-This-String")
                If LocationId > 0 Then
                'REM A Match Was Found
                Else
                'REM No_Match_Was_Found
                End If

                1 Reply Last reply
                0
                • X xman125

                  Thank you both . It the 'indexof' function also not working .... it gives a true result may one time in 20 (when I know it is a true hit). I think there may be a bug in vb.net. It doesn't matter what I do, it comes up with -1 all the time.

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  I'll go so far as to guarantee that there is no bug in INSTR or IndexOf. It may be a match to you, but the function is telling you otherwise. Without seeing REAL examples of the strings you are searching and the strings you are searching for in them, it's impossible to tell you why you think they work and the computer is saying they don't.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  1 Reply Last reply
                  0
                  • X xman125

                    Is there an alternative method ?.... .... I could spend 5 years tyring to get this one working - it's driving me crazy.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    .NET is a widely used framework, and looking for the position in a string is something most of us do. If there was a bug, it'd be found by now. For these types of bugs, the answer is that "SELECT is not broken".

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                    1 Reply Last reply
                    0
                    • X xman125

                      Cannot get instr function to work .net? Using VB.net For some reason, the 'instr' function and the 'string.indexof' is the same. The problem is that one of the 2 string arguments, is a variable that is passed from the calling subroutine (not the literal characters) and it does not recognise the 'match'. When I type in the literal characters with " " -- it works ok. Here is a copy of the line that is not working. 'm' is the positional marker / 'largetxt' the body that is being searched and 'smalltxt' is the string that is being searched for. intx = InStr(m, mainTxt, smallTxt) The variable 'smallTxt' is the problem ---- if I have: intx = InStr(m, mainTxt, "house") --- it works ok Also ... same thing for: intx = largeTxt.IndexOf(smallTxt, m) Cheers

                      M Offline
                      M Offline
                      Mike Meinz
                      wrote on last edited by
                      #10

                      Is this a "case" problem? Try converting both strings to either all upper or all lower case:

                      largeTxt.tolower.IndexOf(smallTxt.tolower, m)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups