[英]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.
我必须编写一个程序,要求用户提供销售员的姓名缩写(C,D或M)以及他们进行的销售的价格。 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. 当我点击计算佣金时,它当然应该计算佣金(确实如此),但是因为变量是方法级别(或某些较低的访问级别),而且我似乎无法返回它们,所以我无法传递第二个方法从“ ... C”变量中获取所需的信息。
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? 对我而言,最简单的方法是获取btnDisplayCommission_Click事件所需的信息?
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: 您也可以在btnDisplayCommission_Click方法中计算值:
MessageBox.Show("Cliff's Commission: " + cliffS * .10 + "\n"
+ "Dino's Commission: " , "Total Commissions");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.