Jump to content

Indexing Issue - Dictionary


Recommended Posts

I'm getting an index out IndexOutOfRange Exception, here is my code in vb6:

Public Function GetDelimitedWord(ByVal pvstrInput As String, _
                                 ByVal pvintItem As Integer, _
                                 ByVal pvstrDelim As String) As String

    Const lcstrPROC_NAME As String = "GetDelimitedWord"

    Dim lvintCounter As Integer
    Dim lvintNewStartPos As Integer
    Dim lvintStartPos As Integer
    Dim lvintEndPos As Integer

    On Error GoTo ErrorHandler

    lvintCounter = 1
    lvintStartPos = 1

    For lvintCounter = 2 To pvintItem
        lvintNewStartPos = InStr(lvintStartPos, pvstrInput, pvstrDelim) + 1

        If lvintNewStartPos < lvintStartPos Then
            lvintStartPos = Len(pvstrInput) + 1
            Exit For
            lvintStartPos = lvintNewStartPos
        End If

    Next lvintCounter

    lvintEndPos = InStr(lvintStartPos, pvstrInput, pvstrDelim) - 1

    If lvintEndPos <= 0 Then
        lvintEndPos = Len(pvstrInput)
    End If

    GetDelimitedWord = Mid$(pvstrInput, lvintStartPos, lvintEndPos - lvintStartPos + 1)

    Exit Function
    NonContextErrorRaise mcstrMODULE_NAME, lcstrPROC_NAME, Err.Number, Err.Source, Err.Description
    Resume ExitHandler
End Function

And here is the code generated by the tool:

internal static string GetDelimitedWord(string pvstrInput, int pvintItem, string pvstrDelim)
  const string lcstrPROC_NAME = "GetDelimitedWord";
  string result2 = "";

  int lvintCounter = 0;
  int lvintNewStartPos = 0;
  int lvintStartPos = 0;
  int lvintEndPos = 0;


    lvintCounter = 1;
    lvintStartPos = 1;

    int tempForEndVar = pvintItem;
    for (lvintCounter = 2; lvintCounter <= tempForEndVar; lvintCounter++)
      lvintNewStartPos = Strings.InStr(lvintStartPos, pvstrInput, pvstrDelim, CompareMethod.Binary) + 1;

      if (lvintNewStartPos < lvintStartPos)
        lvintStartPos = Strings.Len(pvstrInput) + 1;
        lvintStartPos = lvintNewStartPos;


    lvintEndPos = Strings.InStr(lvintStartPos, pvstrInput, pvstrDelim, CompareMethod.Binary) - 1;

    if (lvintEndPos <= 0)
      lvintEndPos = Strings.Len(pvstrInput);

    result2 = pvstrInput.Substring(lvintStartPos - 1, Math.Min(lvintEndPos - lvintStartPos + 1, pvstrInput.Length - (lvintStartPos - 1)));
  catch (Exception theException)


  return result2;


I have similar code in over 100 places in my code, how can I solve it?


Link to comment
Share on other sites

  • Mobilize.Net Staff

Hi Charlie,

Thanks for your question, 

I'm part of the Support Team.

I was testing your code, but I was not able to replicate the exception: "indexOutOfRange", or similar, 
In other words, your source code works properly, however, Could you please help us doing an example? (Just the parameters that you are sending to generate the issue)

Kind Regards

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Terms of Use