在我的Windows窗体中,我有2个文本框,即启动里程表读数和结束里程表读数。我的目标是从“结束阅读”中减去“开始阅读”,并在窗体表格标签中显示客户名称和电话号码旁边的标签中的差异。
如何返回方法getMilesCharge()
的值并将其显示在confirmLabel
上?
租车类代码
//A class that represents the Rental Agency Class.
namespace Assignment1
{
partial class RentalAgencyClass
{
//instance variables
public string customerName { get; set; }
public string phoneNumber { get; set; }
public double sMiles { get; set; }
public double eMiles { get; set; }
public double noOfDays { get; set; }
private double DAY_CHARGE = 15;
private double MILE_CHARGE = 0.12;
//Constructor class
//sets the value of the starting and ending miles.
//sets the value of the number of days the car was rented for
public RentalAgencyClass(double startMiles, double endMiles, double days)
{
startMiles = sMiles;
endMiles = eMiles;
days = noOfDays;
}
//method to calculate the number of miles driven on the rental
public double getMileCharge()
{
double milesDriven = 0;
milesDriven = eMiles - sMiles;
return milesDriven * MILE_CHARGE;
}
//method to calculate the Day Charges on the rental
public double getDayCharge()
{
return noOfDays * DAY_CHARGE;
}
//Property to display the information on the label
public string GetInfo()
{
return customerName + " | " + phoneNumber + " | " + getDayCharge() +" miles";
}
}
}
表单设计器类代码
namespace Assignment1
{
public partial class RentalAgencyClass : Form
{
RentalAgencyClass aCarRental;
public RentalAgencyClass()
{
InitializeComponent();
}
private void calculateButton_Click(object sender, EventArgs e)
{
try
{
//instantiates object
aCarRental = new RentalAgencyClass();
aCarRental.customerName = nameTextBox.Text;
aCarRental.phoneNumber = phoneTextBox.Text;
//aCarRental. = getDayCharge();
// aCarRental.milesDriven = //store the difference in this variable
//displayLabel.Text = "(student information saved)";
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error");
}
//Displays information about the Rental
confirmLabel.Text = aCarRental.GetInfo();
}
}
}
答案 0 :(得分:0)
通过在aCarRental = new RentalAgencyClass();
方法中调用calculateButton_Click
,您正在调用partial class RentalAgencyClass
的无参数构造函数,这意味着在您的情况下,您正在创建表单的新实例而不是设置你的财产。因此sMiles
和eMiles
将保留其默认值0
。
要使代码正常工作,您必须执行几个步骤。
首先,我建议您拆分表格和代理机构。
因此,假设您将表单类重命名为RentalCalculator
。下一步,您必须/可以从partial
中移除RentalAgencyClass
,因为它不再是您的表单类的一部分,我认为您不希望在另一部分扩展您的类你的代码。
正如LarsTech在评论中指出的那样。您现在应该将RentalAgencyClass
构造函数修复为:
public RentalAgencyClass(double startMiles, double endMiles, double days)
{
this.sMiles = startMiles;
this.eMiles = endMiles;
this.noOfDays = days;
}
并可以将以下属性添加到您的班级
public double milesDriven
{
get
{
return this.eMiles - this.sMiles;
}
}
至少你必须改变你的事件处理程序:
private void calculateButton_Click(object sender, EventArgs e)
{
try
{
// if not existing you have to create some input textboxes
double startMiles = Convert.ToDouble(startMilesTextBox.Text);
double endMiles = Convert.ToDouble(endMilesTextBox.Text);
double days = Convert.ToDouble(daysTextBox.Text);
// Hint: you are creating a new instance on every button click
// and overwriting your field in your form class.
aCarRental = new RentalAgencyClass(startMiles, endMiles, days);
aCarRental.customerName = nameTextBox.Text;
aCarRental.phoneNumber = phoneTextBox.Text;
// Store the result in local variables
// if you want to do something with them later
double dayCharge = aCarRental.getDayCharge();
double milesCharge = aCarRental.getMilesCharge();
double drivenMiles = aCarRental.milesDriven;
// displayLabel.Text = "(student information saved)";
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error");
}
//Displays information about the Rental
confirmLabel.Text = aCarRental.GetInfo();
}
回答你的问题:
如何返回方法
getMilesCharge()
的值并将其显示在confirmLabel
上?
您必须在calculateButton_Click
方法中更改以下行:
confirmLabel.Text = aCarRental.GetInfo();
为:
confirmLabel.Text = aCarRental.getMilesCharge().ToString();
最后但并非最不重要的是,让我给你一个善意的建议 您可以查看Microsoft Naming Guidelines 例如:属性应在PascalCasing中命名 但这只是我个人的意见。