Greener
Greener

Reputation: 1387

How to access a variable from a seperate event handler

I've been working on a lab for school and everything was going swimmingly until I hit a snag. I have to write a program that asks the user for a salesperson's initial (C, D, or M) and the price of the sale they made. I also have to keep a running tally of the commission earned by each salesman until I hit clear, or exit the program.

When I hit the calculate commission, it of course should calculate the commission (which it does), but because the variables are method level (or some lower access level) and I can't seem to return them I can't pass the 2nd method the information it needs from the "...C" variables.

What the user would hypothetically see if they ran the program

namespace SunshineHotTubs
{
    public partial class FrmSalesCommission : Form
    {
        double cliffS = 0;
        double dinoS = 0;
        double marshaS = 0;

        public FrmSalesCommission()
        {
            InitializeComponent();
        }

        public void btnCalcCommission_Click(object sender, EventArgs e)
        {
            string salesperson = txtInitial.Text;
            double sale = Convert.ToDouble(txtSaleTotal.Text);

            switch(salesperson)
            {
                case "c":
                case "C":
                    cliffS += sale;
                    double cliffC = cliffS * .10;
                    lblCommission.Text = Convert.ToString(cliffC);
                    break;
                case "d":
                case "D":
                    dinoS += sale;
                    double dinoC = dinoS * .10;
                    lblCommission.Text = Convert.ToString(dinoC);
                    break;
                case "m":
                case "M":
                    marshaS += sale;
                    double marshaC = marshaS * .10;
                    lblCommission.Text = Convert.ToString(marshaC);
                    break;
            }
        }

        private void btnDisplayCommission_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Cliff's Commission: " + cliffC + "\n"
                            + "Dino's Commission: " , "Total Commissions");
        }
    }
}

What's the simplest way for me the get the information I need to my btnDisplayCommission_Click event?

Upvotes: 0

Views: 1197

Answers (1)

Marat Faskhiev
Marat Faskhiev

Reputation: 1302

You already declared variables at class level:

double cliffC = 0;
double dinoC = 0;
double marshaC = 0;

Just replace this:

double cliffC = cliffS * .10;
double dinoC = dinoS * .10;
double marshaC = marshaS * .10;

On this:

cliffC = cliffS * .10;
dinoC = dinoS * .10;
marshaC = marshaS * .10;

Update

Also you can just calcuate values in btnDisplayCommission_Click method:

MessageBox.Show("Cliff's Commission: " + cliffS * .10 + "\n"
                + "Dino's Commission: " , "Total Commissions");

Upvotes: 1

Related Questions