我遇到了一個問題,在這個程序中,我不能讓我的Calcpay方法輸出在正確的位置,它的輸出錯誤。我希望它在點擊computepay
按鈕時輸出,但它不會識別那裏的對象,當我把textbox7
置於openToolStripMenuItem1_Click
下測試它時,它反而出現爲0。 預先感謝任何幫助C#類的問題 - 對象不被識別
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//opens about page
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
}
//closes program
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
//reads in data from text file, assigns to textboxes.
private void openToolStripMenuItem1_Click(object sender, EventArgs e)
{
double emphours;
double hourpay;
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "text files (*.txt)|*txt";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
StreamReader data = new StreamReader(myStream);
textBox1.Text = data.ReadLine();
textBox4.Text = data.ReadLine();
textBox2.Text = data.ReadLine();
textBox3.Text = data.ReadLine();
emphours = double.Parse(data.ReadLine());
hourpay = double.Parse(data.ReadLine());
Employee employ = new Employee(emphours, hourpay);
textBox5.Text = Convert.ToString(employ.EmpHours);
textBox6.Text = Convert.ToString(employ.EmpHourPay);
}
}
}
// The CalcPay_Clickmethod
// display employee net pay
private void computepay_Click(object sender, EventArgs e)
{
// need to get calcpay method to output here
//textbox7.Text = xx
}
}
}
public class Employee
{
const int TimeAndAHalfMarker = 40;
const double Federaltax = .20;
const double Statetax = .075;
const double TimeAndAHalf = 0.5;
private double netpay, grossPay, stateTax, federalTax;
public double EmpHours { get; private set; }
public double EmpHourPay { get; private set; }
public Employee(double emphours, double emphourpay)
{
EmpHours = emphours;
EmpHourPay = emphourpay;
}
public double Calcpay(double emphours, double emphourpay)
{
EmpHours = emphours;
EmpHourPay = emphourpay;
if (EmpHours > TimeAndAHalfMarker)
grossPay += (((EmpHours - TimeAndAHalfMarker) * EmpHourPay) * TimeAndAHalf);
federalTax = grossPay * Federaltax;
stateTax = grossPay * Statetax;
netpay = grossPay - stateTax - federalTax;
return netpay;
}
}
}
示例文本文件是:
瑪麗R.程序員
17北榆大道
789-1000
40.0
15.00
這是有史以來問題的最佳標題...你有很多意見。 – 2011-06-05 21:53:08