none
Array Problem RRS feed

  • Question

  • I'm not sure how to fix this problem in my code. I have been stuck on it four hours. It said the index was outside the bounds of the array. It only happens when i select anything in the pastrielistbox. The error shows up in this line:             order.price = Convert.ToDouble(CustOrder.Split('$')[1]);

    Namespace CoffeeShop

    {
        public partial class Menu : Form
        {
            public Menu()
            {
                InitializeComponent();
            }

            public struct Orders
            {
                public string item;
                public double price;
            }
            Orders order = new Orders();
            static double total = 0;
            string TotalBill = "Final Bill : \n";

            private void GetValues(string CustOrder)
            {
                order.item = CustOrder.Split('$')[0];
                order.price = Convert.ToDouble(CustOrder.Split('$')[1]);
                BillListBox.Items.Add("Price: " + order.price);
                TotalBill += "Ordered Item: " + order.item + "\nPrice: " + order.price.ToString("C2") + "\n";
                UpdateBill();
            }

            private void UpdateBill()
            {
                total += order.price;
                BillListBox.Items.Clear();
                BillListBox.Items.AddRange(TotalBill.Split('\n'));
                BillListBox.Items.Add("Total: " + total.ToString("C2"));
            }
            private void ClearButton_Paint(object sender, PaintEventArgs e)
            {
                ControlPaint.DrawBorder(e.Graphics, ClearButton.ClientRectangle,
                SystemColors.ControlLight, 5, ButtonBorderStyle.Outset,
                SystemColors.ControlLight, 5, ButtonBorderStyle.Outset,
                SystemColors.ControlLight, 5, ButtonBorderStyle.Outset,
                SystemColors.ControlLight, 5, ButtonBorderStyle.Outset);
            }

            private void ClearButton_Click(object sender, EventArgs e)
            {
                ClearButton.Enabled = false;
                CoffeeAdditions.Enabled = false;
                TeaTab.Enabled = false;
                CoffeeList.Enabled = true;
                TeaList.Enabled = true;
                MenuTab.Enabled = true;
                foreach (int i in CoffeeList.CheckedIndices)
                {
                    CoffeeList.SetItemCheckState(i, CheckState.Unchecked);
                }
                foreach (int i in TeaList.CheckedIndices)
                {
                    TeaList.SetItemCheckState(i, CheckState.Unchecked);
                }
                foreach (int i in PastrieList.CheckedIndices)
                {
                    PastrieList.SetItemCheckState(i, CheckState.Unchecked);
                }
            }

            private void CoffeeList_SelectedIndexChanged(object sender, EventArgs e)
            {
                ClearButton.Enabled = true;
                CoffeeAdditions.Enabled = true;
                CartButton.Enabled = true;
                CoffeeList.Enabled = false;
            }

            private void TeaList_SelectedIndexChanged(object sender, EventArgs e)
            {
                CartButton.Enabled = true;
                ClearButton.Enabled = true;
                TeaList.Enabled = false;
                TeaAdditions.Enabled = true;
            }

            private void PastrieList_SelectedIndexChanged(object sender, EventArgs e)
            {
                CartButton.Enabled = true;
                ClearButton.Enabled = true;
                CartButton.Enabled = true;
                PastrieList.Enabled = false; 
            }

            private void CartButton_Click(object sender, EventArgs e)
            {
                if (CoffeeList.SelectedIndex != -1)
                {
                    if (CoffeeAdditions.SelectedIndex != -1)
                    {
                        GetValues(CoffeeList.SelectedItem.ToString());
                        GetValues(CoffeeAdditions.SelectedItem.ToString());
                        CoffeeList.Enabled = true;
                        CoffeeAdditions.Enabled = false;
                        foreach (int i in CoffeeList.CheckedIndices)
                        {
                            CoffeeList.SetItemCheckState(i, CheckState.Unchecked);
                        }

                    }
                    else
                    {
                        MessageBox.Show("Are you sure you would not like to proceed without altering your Coffee?", "Add to Cart", MessageBoxButtons.YesNo);
                        if (MessageBox.Show("Add To Cart?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            GetValues(CoffeeList.SelectedItem.ToString());
                            CoffeeList.Enabled = true;
                            foreach (int i in CoffeeList.CheckedIndices)
                            {
                                CoffeeList.SetItemCheckState(i, CheckState.Unchecked);
                            }
                        }
                        else
                        {

                        }
                    }
                }
                else if (TeaList.SelectedIndex != -1)
                {
                    if (TeaAdditions.SelectedIndex != -1)
                    {
                        GetValues(TeaList.SelectedItem.ToString());
                        GetValues(TeaAdditions.SelectedItem.ToString());
                        TeaList.Enabled = true;
                        TeaAdditions.Enabled = false;
                        foreach (int i in TeaList.CheckedIndices)
                        {
                            TeaList.SetItemCheckState(i, CheckState.Unchecked);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Are you sure you would not like to proceed without altering your Tea?", "Add to Cart", MessageBoxButtons.YesNo);
                        if (MessageBox.Show("Add To Cart?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            GetValues(TeaList.SelectedItem.ToString());
                            TeaList.Enabled = true;

                            foreach (int i in TeaList.CheckedIndices)
                            {
                                TeaList.SetItemCheckState(i, CheckState.Unchecked);
                            }
                            foreach (int i in TeaAdditions.CheckedIndices)
                            {
                                TeaAdditions.SetItemCheckState(i, CheckState.Unchecked);
                            }
                        }
                        else
                        {

                        }
                    }
                }
                else if (PastrieList.SelectedIndex != -1)
                {
                    GetValues(PastrieList.SelectedItems.ToString());
                    foreach (int i in PastrieList.CheckedIndices)
                    {
                        PastrieList.SetItemCheckState(i, CheckState.Unchecked);
                    }
                }
            }
        }
    }


    Conner Ferguson

    Saturday, February 3, 2018 5:42 AM

Answers

  • This happens when you don't have any elements inside the array , do a length check before you assign the value

     order.item = CustOrder.Split('$')[0];
    var myString = CustOrder.Split('$');
    if(myString.Length > 1){
      order.price = Convert.ToDouble(myString[1]);
    }



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (kokkisajee) or Facebook (sajeetharan) profile for Sajeetharan at Stack Overflow, Q&A for professional and enthusiast programmers


    • Edited by Sajeetharan sMVP Saturday, February 3, 2018 6:50 AM
    • Marked as answer by cpdf20 Saturday, February 3, 2018 4:42 PM
    Saturday, February 3, 2018 6:49 AM

All replies

  • This happens when you don't have any elements inside the array , do a length check before you assign the value

     order.item = CustOrder.Split('$')[0];
    var myString = CustOrder.Split('$');
    if(myString.Length > 1){
      order.price = Convert.ToDouble(myString[1]);
    }



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (kokkisajee) or Facebook (sajeetharan) profile for Sajeetharan at Stack Overflow, Q&A for professional and enthusiast programmers


    • Edited by Sajeetharan sMVP Saturday, February 3, 2018 6:50 AM
    • Marked as answer by cpdf20 Saturday, February 3, 2018 4:42 PM
    Saturday, February 3, 2018 6:49 AM
  • Your problem is in this line.

    GetValues(PastrieList.SelectedItems.ToString());

    Here, you are dealing with SelectedItems (plural) instead of SelectedItem (singular) like you are everyhere else. If you try to do a ToString on a collection, the result will be something like "A Collection", which is not what you want. You need to loop through the collection and call GetVlue on each item separately.


    • Edited by Ante Meridian Saturday, February 3, 2018 8:32 AM ToString, not GetString (doh!)
    Saturday, February 3, 2018 8:30 AM