Results 1 to 10 of 10

Thread: Please help w/ Debugging Index Out of Bounds

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Please help w/ Debugging Index Out of Bounds

    I am posting extensive details and code here, including the runtime error message I receive that keeps killing my application. I am about to pull my hair out because I cannot find anything wrong with the code, and I do not get the error when debugging the application in VB! Basically a user enters some medical information on a form in text boxes and adds some medical conditons and medications into two listbox controls on the form, on for medical problems and one for medications they are on. When Button1 (command button) is clicked, it calls a subroutine stored in a module1. Then the application throws and Index out of Bounds error and crashes, losing all information that was entered. Here is the code on Button1:

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext5.Click
    
    Call Store_MedData
    
    End Sub
    Here is the code for Store_MedData in the module. The error occurs on red text:

    Code:
    Dim ClientLastName as string, ClientMI as String, ClientFirstName as string, ClientAddress as String, _
    ClientAddress2 as String, ClientCity as String, ClientState as String, ClientZip as String, ClientDOB as String, _
    ClientBloodType as String,ClientActiveMeds(frmmain.lstActiveMeds.Items.Count - 1) As String, _
    ClientActiveMedProbs(frmmain.lstActiveMedProbs.Items.Count - 1) as String
     
    Public Sub Store_MedData()
    
            'Store all client information as string values
            ClientLastName = Trim(frmmain.txtLastname.Text)
            ClientMI = Trim(frmmain.txtMI.Text)
            ClientFirstname = Trim(frmmain.txtFirstname.Text)
            ClientAddress = Trim(frmmain.txtAddress1.Text)
            ClientAddress2 = Trim(frmmain.txtAddress2.Text)
            ClientCity = Trim(frmmain.txtCity.Text)
            ClientState = Trim(frmmain.cmbState.Text)
            ClientZip = Trim(frmmain.mtxtZip.Text)
            ClientDOB = Trim(frmmain.mtxtDOB.Text)
            ClientBloodType = Trim(frmmain.cmbBloodType.Text)
    
            'Store the path of the client photo
            ClientPhotoPath = Trim(frmmain.openPic.FileName)
    
            'store all active medications information in an array & build a string
            For i As Integer = 0 To frmmain.lstActiveMeds.Items.Count - 1
                ClientActiveMeds(i) = frmmain.lstActiveMeds.Items.Item(i).ToString
            Next
    
            If frmmain.lstActiveMeds.Items.Count = 0 Then
                ActiveMedsResult = ""
            Else
                ActiveMedsResult = String.Join(", ", ClientActiveMeds)
                ActiveMedsResult = Left(ActiveMedsResult, Len(ActiveMedsResult) - 2)
            End If
    
    
    
            'store all active medical problem information in an array & build a string
            For i As Integer = 0 To frmmain.lstActiveMedProbs.Items.Count - 1
                ClientActiveMedProbs(i) = frmmain.lstActiveMedProbs.Items.Item(i).ToString
            Next
    
            If frmmain.lstActiveMedProbs.Items.Count = 0 Then
                ActiveMedProbsResult = ""
            Else
                ActiveMedProbsResult = String.Join(", ", ClientActiveMedProbs)
                ActiveMedProbsResult = Left(ActiveMedProbsResult, Len(ActiveMedProbsResult) - 2)
            End If
    
    End Sub
    AND, here is the Runtime error message when running the application compiled and installed on a workstation. I don't see how or why the index out of bounds error is occurring. It should never count anywhere outside of the number of items in the listbox according to the code:

    Code:
    See the end of this message for details on invoking 
    just-in-time (JIT) debugging instead of this dialog box.
    
    ************** Exception Text **************
    System.IndexOutOfRangeException: Index was outside the bounds of the array.
       at MYAPP.Medical.Store_Data()
       at MYAPP.frmmain.Button1_Click(Object sender, EventArgs e)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    
    
    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3074 (QFE.050727-3000)
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    ----------------------------------------
    MyAPP
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///C:/Program%20Files/MYAPP/MYAPP.exe
    ----------------------------------------
    Microsoft.VisualBasic
        Assembly Version: 8.0.0.0
        Win32 Version: 8.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
    ----------------------------------------
    System
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.Runtime.Remoting
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
    ----------------------------------------
    System.Configuration
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3074 (QFE.050727-3000)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    AxInterop.csXImage
        Assembly Version: 3.4.0.0
        Win32 Version: 3.4.0.0
        CodeBase: file:///C:/Program%20Files/MYAPP/AxInterop.csXImage.DLL
    ----------------------------------------
    Interop.csXImage
        Assembly Version: 3.4.0.0
        Win32 Version: 3.4.0.0
        CodeBase: file:///C:/Program%20Files/MYAPP/Interop.csXImage.DLL
    ----------------------------------------
    
    ************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (machine.config) must have the
    jitDebugging value set in the system.windows.forms section.
    The application must also be compiled with debugging
    enabled.
    
    For example:
    
    <configuration>
        <system.windows.forms jitDebugging="true" />
    </configuration>
    
    When JIT debugging is enabled, any unhandled exception
    will be sent to the JIT debugger registered on the computer
    rather than be handled by this dialog box.
    The application is being run on a PC running Windows Vista Home Premium w/ SP1. (aka. Windows "Worthless Edition")

    Any help getting this issue resolved is extremely appreciated as it is driving me crazy and is the only thing keep my application from being used. I have been trying to resolve this issue for over a month now with no success. Thanks in advance for any insight.

    -Jeremy

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Please help w/ Debugging Index Out of Bounds

    are you adding any items to the listbox during execution of your program?

    you need to be sure that the upperbound of the array = items.count -1

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Please help w/ Debugging Index Out of Bounds

    Yes, the user enters the name of medication into a textbox and then clicks a button to add it to the listbox. The number of items in the listboxes will be variable. My arrays are declared here as you mentioned:

    Code:
    Dim ClientLastName as string, ClientMI as String, ClientFirstName as string, ClientAddress as String, _
    ClientAddress2 as String, ClientCity as String, ClientState as String, ClientZip as String, ClientDOB as String, _
    ClientBloodType as String,ClientActiveMeds(frmmain.lstActiveMeds.Items.Count - 1) As String, _
    ClientActiveMedProbs(frmmain.lstActiveMedProbs.Items.Count - 1) as String

  4. #4
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Please help w/ Debugging Index Out of Bounds

    You need to REDIM PRESERVE your arrays everytime an item is added, otherwise, your list will grow, and the array will not.

    Instead of using arrays, I recommend using a List(of string) instead. It's more fool-proof (but they always make a better fool!)

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Please help w/ Debugging Index Out of Bounds

    did that solve your problem?

    don't forget to mark the thread resolved

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Please help w/ Debugging Index Out of Bounds

    Something like this?

    Code:
     'store all active medications information in an array & build a string
            For i As Integer = 0 To frmamenmain.lstActiveMeds.Items.Count - 1
                ClientActiveMeds(i) = frmamenmain.lstActiveMeds.Items.Item(i).ToString
                ReDim Preserve ClientActiveMeds(i)
            Next
    Thanks.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Please help w/ Debugging Index Out of Bounds

    vb Code:
    1. frmmain.lstActiveMeds.Items.add("whatever")
    2. ReDim Preserve ClientActiveMeds(frmmain.lstActiveMeds.Items.Count - 1)

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Please help w/ Debugging Index Out of Bounds

    Ok, I am confused now because the subroutine Store_MedData is in a module. The Arrays are declared in the module, but the button_1 is actually on the main form. So I will not be able to reference the array from outside of the module. What do i need to search for on List. If there is a better way to do this, I'd rather just re-code it and have it work than chase my tail forever trying to fix what I've got now. Thanks for the help.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Please help w/ Debugging Index Out of Bounds

    declare the array public instead of dim'ing it

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Please help w/ Debugging Index Out of Bounds

    I still cannot make this work. All I am trying to do is loop through all of the items in a listbox, and build a string.

    Listbox Contents:

    MedicineA
    MedicineB
    MedicineC
    MedicineD

    I need to build a string like below, separating the medicine with a comma and a space:

    MedicineA, MedicineB, MedicineC, MedicineD

    The only variable is the number of items in the listbox. Is there an easier way to do this.

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