BURTON JOHN
BURTON JOHN

Reputation: 85

How to fix index out of range

I have the following code. and I keep geting an error "Index out of range when I purposely enter the wrong combox productnumber. "index out of range here.... ProductSalesTotalDecimal(IndexInteger) += (txtPriceAmount.Text * txtQuantityAmount.Text)"

this only does this after I click the combobox down arrow to pull in the right number but then backspace to change it to be wrong. otherwise when I start the program and enter the numbers manually into combox in validates and works fine. Any suggestions on how to fix?

  Private Sub PurchaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As   System.EventArgs) Handles PurchaseToolStripMenuItem.Click

    'Test to determine if a product was found.
    If txtDescription.Text = String.Empty Then

        'Cannot purchase, product was not found
        MessageBox.Show("You must select a valid product before purchasing.", "Cannot Purchase", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        txtProductID.Focus()
        txtProductID.SelectAll()
    Else
        'Can purchase the product
        'Build a string to display in the listbox control

        Dim ProductString As String = txtProductID.Text.PadRight(12, " ") & "" & txtDescription.Text.PadRight(50, " ") & "" & txtQuantityAmount.Text.PadRight(7, " ") & "" & txtPriceAmount.Text.PadLeft(9, " ").ToString
        lstPurchaseItems.Items.Add(ProductString).ToString()
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Accumulate the total value of this customer order
        'and display it to the output textbox
        TotalDueDecimal += (txtPriceAmount.Text.ToString * txtQuantityAmount.Text)
        txtTotalDueAmount.Text = TotalDueDecimal.ToString("C2")
        'TotalDueTextBox.Text = QuantityTextBox.Text * TotalDueDecimal.ToString("C2")

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


        'Accumulate total sales by product to an array
        Dim IndexInteger As Integer = cboProductIDLookup.SelectedIndex
        ProductSalesTotalDecimal(IndexInteger) += (txtPriceAmount.Text * txtQuantityAmount.Text)

        'Here you can clear the form of product info if you think
        'that is a good way to do the processing
        cboProductIDLookup.SelectedIndex = -1
        txtProductID.Clear()
        txtDescription.Clear()
        txtPriceAmount.Clear()
        txtQuantityAmount.Clear()
        txtProductID.Focus()
    End If
End Sub

Upvotes: 0

Views: 1882

Answers (2)

  'Accumulate total sales by product to an array
  Dim IndexInteger As Integer = cboProductIDLookup.SelectedIndex

There is no 'program error' simply a condition you did not account for. If the control list DropDowntype is set to DropDown you can pick something from the list OR type something in. In some apps, typing in something new makes that item get added to the datasource.

In that case, or when the user 'enters a wrong' value, the combo.SelectedIndex will be -1. This is by design and is easy to test for:

   ' you missed this 
   If cboProductIDLookup.SelectedIndex = -1 Then
      ' Post error/warning message
      ' or
      ' add new item
      ' as appropritate
   End If

In some apps it simply is not feasible to list every possible option in the list, so only the top likely options are listed. The user can type in something altogether different as a perfectly valid option. In an add new item type app, a SelectedIndex of -1 is the signal to do so.

As you belatedly discovered you can have the combo box work in Limit-to-list fashion meaning the user cannot enter a value not in the list. This is not a fix, but a different operational mode or style. This operational mode is not appropriate for the two other use cases described above.

Upvotes: 1

Sarima
Sarima

Reputation: 749

My personal recommendation would be to always compare lengths before analyzing.

You have 2x combo boxes, each with a variable size.

Try the following idea:

if ((Combobox1.SelectedIndex <= (Combobox2.Items.Count - 1)) and 
(Combobox2.SelectedIndex <= (Combobox1.Items.Count - 1))) then
   //operation
else
   //error
end if

Alternitively, put some throw...catch statements.

Upvotes: 0

Related Questions