Array out of bounds Visual Basic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rhonda F
    New Member
    • May 2011
    • 4

    Array out of bounds Visual Basic

    I am trying to calculate the price of a pizza based on the following table.

    Code:
                  Small   Medium  Large  Monster
    Regular       5.99    6.99    7.99    8.99
    Stuffed Crust 7.99    8.99    9.99   10.99
    I am using a two dimensional array and the following code. I am getting an array out of bounds error. Can anyone help? Thanks in advance!

    Code:
    Dim pizzaArrayDecimal(,) As Decimal = {{5.99, 6.99, 7.99, 8.99}, _
                                      {7.99, 8.99, 9.99, 10.99}}
    
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            'Look up the pizza price
    
            Dim SizeIndexInteger As Integer
            Dim StyleIndexInteger As Integer
    
            SizeIndexInteger = cboSize.SelectedIndex
            StyleIndexInteger = cboStyle.SelectedIndex
    
            If SizeIndexInteger <> -1 And StyleIndexInteger <> -1 Then
                pizzaTextBox.Text = pizzaArrayDecimal(SizeIndexInteger, StyleIndexInteger).ToString("N")
            Else
                MessageBox.Show("Select the Size and Style.", "Information Missing", _
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    
            End If
        End Sub
    Last edited by TheSmileyCoder; May 3 '11, 08:38 AM. Reason: Added [Code] tags around your post. These are mandatory to use on the site.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Hi.

    Apologies for the delay.

    From the look of the syntax, this probably belongs in the VB.Net area. And given that it's been over a month, you've probably figured it out by now.

    In any case, I'd hazard a guess that your problem centres around whether your array and your combo box start counting at 0 or 1. Depending on which is which, you're probably bombing on an index value of either 0 or 4. It should be a simple matter of either changing your array definition, or adjusting the index values up or down by 1 before accessing the array.

    Comment

    Working...