Question

I am trying to make a form where the user will input values to calculate an order at a pizza restaurant. After the user calculates one order, they can make multiple orders after that. So, I am trying to add each order to an array and then pull the array out into another class so that I can view a summary of all of the orders on another form. Here is my code from the first form. None of the arrays are accessible to the other class. May you help me?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace MidTermPizzas
{
    public partial class Form1 : Form
    {
        pizzaOrder aOrder = new pizzaOrder();
        public static ArrayList pizzaAmountArray = new ArrayList();
        public Form1()
        {
            InitializeComponent();
        }

        //click File, Exit
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Enjoy your pizza!");
            this.Close();
        }

        //click View, Summary of Orders Placed
        private void summaryOfOrdersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SummaryOfOrdersPlaced myForm = new SummaryOfOrdersPlaced();
            myForm.Show();
        }

        //form load
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        //text in box to the right of "Amount of Pizzas"
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //ArrayList pizzaAmountArray = new ArrayList();

            int v;
            if (int.TryParse(textBox1.Text, out v))
            {
                aOrder.numberOfPizzas = v;
                pizzaAmountArray.Add(v); //add to array list
            }
            else aOrder.numberOfPizzas = 0;
        }

        //text in box to the right of "Amount of Cokes"
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            ArrayList cokeAmountArray = new ArrayList();

            int v;
            if(int.TryParse(textBox2.Text, out v))
            {
                aOrder.numberOfCokes = v;
                cokeAmountArray.Add(v); //add to array list
            }
            else aOrder.numberOfCokes = 0;
        }

        //click Calculate Amount Due
        private void calculateAmountDue_Click(object sender, EventArgs e)
        {
            textBox3.Text = Convert.ToString(aOrder.GetAmountDue());
        }

        //click Calculate Sales Tax
        private void button2_Click(object sender, EventArgs e)
        {
            textBox4.Text = Convert.ToString(aOrder.TaxDue());
        }

        //text in box to the right of "Amount Paid"
        private void textBox5_TextChanged_1(object sender, EventArgs e)
        {
            ArrayList totalAmountPaidArray = new ArrayList();

            double v;
            if (double.TryParse(textBox5.Text, out v))
            {
                aOrder.getAmountPaid = v;
                totalAmountPaidArray.Add(v); //add to array list
            }
            else aOrder.getAmountPaid = 0;
        }

        //click Calculate Change Due button
        private void calculateChangeDue_Click(object sender, EventArgs e)
        {
            textBox6.Text = Convert.ToString(aOrder.GetChangeDue());
        }


        //reset button
        private void button1_Click_1(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
        }

        }

    }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace MidTermPizzas
{
    class DailySummary
    {
        Form1 aForm1Stuff = new Form1();
        public int numberOfPizzas
        {
            get
            {
                //not sure what to put here
            }
            set
            {
            }
        }
Was it helpful?

Solution

There are some conceptual problems with your code.

You don't add a number (v) to the ArrayList but a pizzaOrder, and you don't react to the text_changed event every time. In WinForms this is called for every character typed. If your user make a mistake and type 3 when he/she means 2 you have already added the number 3 to the array. Also, there are better alternative to the ArrayList class used....

So, first thing remove the global pizzaOrder and change your ArrayList to a List<pizzaOrder>

// pizzaOrder aOrder = new pizzaOrder();
public List<pizzaOrder> pizzasOrdered = new List<pizzaOrder>();

then add a new button called InsertOrder and add this code for its click event

private void InsertOrder_Click_1(object sender, EventArgs e)
{
    // Call a common function that creates an object of pizzaOrder 
    // from the values typed by your user and add it to the List
    pizzaOrder aOrder = MakeAnOrder();
    pizzasOrdered.Add(aOrder);
}

private pizzaOrder MakeAnOrder()
{
    double d;
    int v;
    pizzaOrder aOrder = new pizzaOrder();

    // If zero is an acceptable value for pizza/cokes and amount then
    // you don't need to check the outcome of the tryparse...
    int.TryParse(textBox1.Text, out v);
    aOrder.numberOfPizzas = v;
    int.TryParse(textBox2.Text, out v);
    aOrder.numberOfCokes = v;
    double.TryParse(textBox5.Text, out d);
    aOrder.getAmountPaid = d;
    return aOrder;
 }

Now you can remove the event handler on the textboxes and change the code in the following events

private void calculateAmountDue_Click(object sender, EventArgs e)
{
    textBox3.Text = Convert.ToString(MakeAnOrder().GetAmountDue());
}
.....
// the same code for the other buttons

Finally, when you need to pass the List<pizzaOrder> to another form, just pass the global variable in the constructor of the second form

private void summaryOfOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
    SummaryOfOrdersPlaced myForm = new SummaryOfOrdersPlaced(pizzasOrdered);
    myForm.Show();
}

And receive the List in the constructor of the form

public class SummaryOfOrdersPlaced : Form
{
    public SummaryOfOrdersPlaced(List<pizzaOrder> orders)
    {
        foreach(pizzaOrder o in orders)
           ... use the passed in variable to loop on orders and create your display
    }
}

OTHER TIPS

Add a public array property to class #1 or class #2 so it can be visible from other class

you may pass in instance of arraylist that you want to share between forms as parameter to constructor

All you need to do is access the ArrayList from within your second class and intantiate the property there like so:

public DailySummary()
{
    SomeArrayListProperty = Form1.PizzaAmounArray;
    //do what you want 
}

private ArrayList SomeArrayListProperty {get; set;}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top