Jump to content

Indexing Issue - Dictionary


Charlie

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
        Else
            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)

ExitHandler:
    Exit Function
ErrorHandler:
    NonContextErrorRaise mcstrMODULE_NAME, lcstrPROC_NAME, Err.Number, Err.Source, Err.Description
    Resume ExitHandler
    Resume
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;

  try
  {

    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;
        break;
      }
      else
      {
        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)
  {
    theException.HandleError(theException);

  }

  return result2;
}

 

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

Thanks.

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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • 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