【问题标题】:Passing Private Class to Form将私有类传递给 Form
【发布时间】: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); 应该可以正常工作。

标签: c# private


【解决方案1】:

我不太明白你的问题,我没有读过你提到的书,但据我所知,你的 Intake 课程不是私人的,所以唯一的问题是你试图在你的MessageBox,因此,如果您取消注释该语句,它根本不会编译。 因此,如果您想在 MessageBox 中显示您的实例名称:

MessageBox.Show("Name: " + intake.Name);

肯定会起作用。 您唯一需要记住的(在这种情况下)是私有字段和公共属性之间的区别。 私有字段只能在您的类中访问,公共属性可以由您的所有程序访问。

Accessibility Levels (C# Reference)

公开

访问不受限制。


受保护

访问仅限于包含类或从包含类派生的类型。


内部

访问仅限于当前程序集。


受保护的内部

访问权限仅限于当前程序集或派生自包含类的类型。


私人

访问仅限于包含类型。

【讨论】:

    【解决方案2】:

    在您的 Intake 类中,添加 GetProteinIntake() 方法:

    public class Intake
    {
        ... your existing code ....
    
        public double GetProteinIntake()
        {
            return weight * 0.86;
        }
    }
    

    在您的表单中,将您的 btnCalculate_Click 事件替换为:

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        Intake intake = new Intake(txtName.Text, Convert.ToInt32(txtPatientId.Text), Convert.ToInt32(txtWeight.Text));
        txtProtein.Text = intake.GetProteinIntake().ToString();
    }
    

    【讨论】:

    • 你能详细说明一下吗?我尝试使用它,但返回“错误 CS1503 参数 1:无法从 'double' 转换为 'string'” 我想将它分配给我的文本框,即“txtProtein.Text”
    • 哦,你应该把public int改成public double(没注意数字类型),然后你可以用intake.GetProteinIntake().ToString()分配给文本框。
    • intake.GetProteinIntake().ToString(txtProtein.Text);还是不计算。知道为什么吗?
    • 嘿抱歉...那是txtProtein.Text = intake.GetProteinIntake().ToString()
    • 那也不行……我是不是做错了什么?
    【解决方案3】:

    仅供参考:在“摄入”类中,您可以覆盖 ToString()

    public override string ToString()
    {
        return string.Format("Name: {0}, PatientId: {1}, Weight: {2}", Name, PatientId, Weight);
    }
    

    然后你就可以使用了

    MessageBox.Show(intake.ToString());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多