我已经从另一个类实例化了一个对象,以便使用该类的属性。在按钮事件中一切正常,但是,在按钮事件之外,我收到一个错误,告诉我我的实例化对象被用作一种类型。如果我采用相同的代码并将其剪切并粘贴到按钮事件中,我不会收到错误消息。我不明白发生了什么以及为什么。对象被实例化,无论它是在按钮事件内部还是外部,那么为什么它不能在按钮事件之外工作呢?我需要在表单打开后立即从另一个表单中自动填充这两个标签字段,而不是在单击按钮时。
这是我的代码:
public partial class MeasurementsForm : Form
{
private MeasurementsBOL busObject = new MeasurementsBOL();
//autofill bodyfat and body weight from nutrition form when form opens
busObject.BodyFatB4 = double.Parse(lblBodyFatB4FromNutrition.Text);
busObject.BodyWeightB4 = double.Parse(lblWeightB4FromNutrition.Text);
//default constructor
public MeasurementsForm()
{
InitializeComponent();
busObject.InitializeConnection();
}
//event handler for B4 input data
private void btnEnterMeasurementsB4_Click(object sender, EventArgs e)
{
//convert input data and assign to variables
busObject.ChestMeasurementB4 = double.Parse(txtChestB4.Text);
busObject.WaistMeasurementB4 = double.Parse(txtWaistB4.Text);
busObject.HipsMeasurementB4 = double.Parse(txtHipsB4.Text);
busObject.RightThighB4 = double.Parse(txtRightThighB4.Text);
busObject.LeftThighB4 = double.Parse(txtLeftThighB4.Text);
busObject.RightArmB4 = double.Parse(txtRightArmB4.Text);
busObject.LeftArmB4 = double.Parse(txtLeftArmB4.Text);
//call method to save input data
busObject.SaveB4Data();
//clear text boxes of data
this.txtChestB4.Clear();
this.txtWaistB4.Clear();
this.txtHipsB4.Clear();
this.txtRightThighB4.Clear();
this.txtLeftThighB4.Clear();
this.txtRightArmB4.Clear();
this.txtLeftArmB4.Clear();
//close form
this.Close();
}
这是我在 MeasurementsBOL 类中的两个属性。虽然我没有展示,但是对象已经被实例化了:
//properties for variables
public double BodyFatB4
{
get { return bodyFatB4; }
set { bodyFatB4 = nutritionObject.BodyFatStart;}
}
public double BodyWeightB4
{
get { return bodyWeightB4; }
set { bodyWeightB4 = nutritionObject.BodyWeight; }
}