-2

I need to call some methods from one class to another class in c#. Its different from the other answers that are put up as i cant seem to find the specific answer i am looking for.

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;

namespace IT2127P2_150538B
{
public partial class InputForm : Form
{
    const double cupCakePrice = 1.50;
    int noOfCupcakes, loyaltyPoints;
    double TotalPrice;
    int[] price = new int[15];
    public InputForm()
    {
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form Summaryform = new SummaryForm();
        Summaryform.ShowDialog();

        noOfCupcakes = int.Parse(txtNoOfCupcakes.Text);
        TotalPrice = noOfCupcakes * cupCakePrice;
        if (txtMemId.Text != "")
        {
            if ( noOfCupcakes / 2 > 0)
            {
                loyaltyPoints = (((noOfCupcakes - 1) / 2) * 3) + 1;

            }
            else
            {
                loyaltyPoints = (noOfCupcakes / 2) * 3;
            }
        }
        else
            {
            loyaltyPoints = 0;
            }
        } 
    }
}
}

The code above is the main class where the computation is done. Now i have to display number of cupcakes, loyalty points and total cupcake price in the code below. My problem is that i cant seem to find a way to call the variables in the primary class to the secondary class.

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;

namespace IT2127P2_150538B
{
public partial class SummaryForm : Form
{
    const double cupcakePrice = 1.50;
    public SummaryForm()
    {
        InitializeComponent();
    }

    private void SummaryForm_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }




}
}
Mohan Ram
  • 3
  • 2
  • _"Now i have to display number of cupcakes, loyalty points and total cupcake price in the code below"_ -- okay, and so? Do you have a question? What, _specifically_ are you having trouble with? Your question is almost certainly a duplicate of https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp, but it'd be good if you'd at least explain what you're actually asking. This question is far from clear. – Peter Duniho Jul 23 '16 at 07:47

2 Answers2

0

Assuming that you are calling the "SummaryForm" through "InputForm", then simply overload the constructor of second class i.e. SummaryForm and pass the information through it. i.e.

public SummaryForm(double dblCupcakes, int intLoyaltyPoints, int intTotalCupcakePrice)
{
    InitializeComponent();
}

And then use this constructor when creating the object in InputForm class.

Hope this helps

0

You have two options, either pass the values through the SummaryForm construtor.

public SummaryForm(int cupcakes, int loyaltypoints, double totalprice)

Or make sure your variables are public static. Then you can get their values.

// InputForm class.
public static int noOfCupcakes, loyaltyPoints;
public static double TotalPrice;

// SummaryForm class.
InputForm.noOfCupcakes;
InputForm.loyaltyPoints;
InputForm.TotalPrice;
  • Glad I could help @MohanRam. –  Jul 23 '16 at 07:50
  • I have encountered a problem while using the second code. Its says 'is a "field" but is used like a "type" – Mohan Ram Jul 23 '16 at 08:20
  • Did you call it using a type or an instance? –  Jul 23 '16 at 08:50
  • i pasted your code after the partial class.' – Mohan Ram Jul 23 '16 at 08:53
  • Put it after the SummaryForm Constructor. –  Jul 23 '16 at 09:00
  • Tried it. It doesnt work unfortunately – Mohan Ram Jul 23 '16 at 09:16
  • Okay, then paste your code, please. –  Jul 23 '16 at 09:18
  • ` { public partial class SummaryForm : Form{ InputForm.noOfCupcakes; InputForm.loyaltyPoints; InputForm.TotalPrice; public SummaryForm() { InitializeComponent(); } private void SummaryForm_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { this.Close(); } ` – Mohan Ram Jul 23 '16 at 09:28
  • Here you go: `public partial class SummaryForm : Form { public SummaryForm() { InitializeComponent(); int SummaryFormnoOfCupcakes = InputForm.noOfCupcakes; int SummaryFormloyaltyPoints = InputForm.loyaltyPoints; double SummaryFormTotalPrice = InputForm.TotalPrice; } private void SummaryForm_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { this.Close(); } }` –  Jul 23 '16 at 09:52