【发布时间】:2016-11-27 15:13:36
【问题描述】:
我有一些使用 Murach 的 C# 书籍使用私有类的小经验,但我仍然是一个新手。我正在尝试创建自己的项目,该项目由“患者 ID”“姓名”“体重”和建议的每日蛋白质摄入量(体重 * .86)等式组成。这最终将被制作成一个基本的数据库,但是,现在我什至都在努力显示结果。而且,我做的那堂课,每日蛋白质摄入量的计算也不行。
这是我的课程代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Form1
{
public class Intake
{
private string name;
private int patientId;
private int weight;
//private int proteinIntake = weight * .86;
//Constuctor
public Intake() { }
//Overload Constructor
public Intake(string name, int patientId, int weight)
{
this.Name = name;
this.PatientId = patientId;
this.Weight = weight;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int PatientId
{
get
{
return patientId;
}
set
{
patientId = value;
}
}
public int Weight
{
get
{
return weight;
}
set
{
weight = value;
}
}
public string GetDisplayText(string sep) =>
name + patientId + sep + weight;
}
}
这是我的表单代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Form1
{
public partial class Form1 : Form
{
public static Intake intake = null;
public Form1()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
Intake intake = new Intake(txtName.Text, Convert.ToInt32(txtPatientId.Text), Convert.ToInt32(txtWeight.Text) );
//obviously does not work MessageBox.Show("Name: " + intake.name);
}
}
}
【问题讨论】:
-
执行
MessageBox.Show("Name: " + intake.name);将不起作用,但执行MessageBox.Show("Name: " + intake.Name);应该可以正常工作。