如何在私有类中执行等式,并将其传递以添加到客户表单中 [英] How do I do an equation in a private class, and pass it to be added to a customer form

查看:66
本文介绍了如何在私有类中执行等式,并将其传递以添加到客户表单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



现在我可以做我想要找到的等式并将它显示在我的添加新患者表格上。但我知道我这样做的方式不会将价值传递给我正在制作的实际患者记录表。



首先,我将展示我的班级



Hello,

Right now I can do the equation I want just find and have it show on my "Add New Patient" form. But I know the way I am doing it will not have the value passed to the actual "Patient Records form that I am building.

First, I will show my class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PatientRecords
{
    public  class Intake
    {
        private string name;
        private int patientId;
        private int weight;
        

        //Constuctor
        public Intake() { }

        //Overload Constructor
        public Intake(string name, int patientId, int weight)
        {
            this.Name = name;
            this.PatientId = patientId;
            this.Weight = weight;
            

        }
        public double GetProteinIntake()
        {
          
            return weight * .86;
        }

        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 override string ToString()
       {
           return string.Format("Name: {0}, PatientId: {1}, Weight: {2}", Name, PatientId, Weight);
       }
    }
}





编辑:我会诚实地说我还是更多现在输了。我一直在学习一本书Murach C#2015,我唯一的体验是使用lambda运算符和表达体。我想我理解你在一堂课中所做的一切都是在说什么,但这在我的工作方式上是新的。这只是你发布的代码



使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;

使用System.Windows.Forms;



名称空间PatientForm

{



公共部分类患者:表格

{



//注释掉,除了我在我的摄影表格中使用它之外,我不明白这一点,其他用户以浓缩的方式教我这个。公共病人{得到;私人集; }

公共病人()

{

InitializeComponent();

}





private void btnSave_Click(object sender,EventArgs e)

{

{

int patId;

if(!int.TryParse(txtPatientId.Text,out patId))

{

MessageBox.Show( 请输入有效的患者ID);

返回;

}

int patWeight;

if( !int.TryParse(txtWeight.Text,out patWeight))

{

MessageBox.Show(请输入有效的患者体重);

返回;

}

患者=新患者(txtName.Text,patId,patWeight);

}

}

private void btnExit_Click(object sender,EventArgs e)

{

this.Close();

}

}

}



我的尝试:



这是我的添加患者表格。





I will be honest and say I am even more lost now. I have been learning from a book "Murach C# 2015" and the only experience I had was using a lambda operator and expression body. I think I understand what you're saying in the sense of doing everything in one class, but this is competely new, in how I am doing it. This is just the code you posted

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 PatientForm
{

public partial class Patient : Form
{

// commented out, don't understand this except how I used it in my intake form, which the other user taught me this in a condensed way. public Patient { get; private set; }
public Patient()
{
InitializeComponent();
}


private void btnSave_Click(object sender, EventArgs e)
{
{
int patId;
if (!int.TryParse(txtPatientId.Text, out patId))
{
MessageBox.Show("Please enter a valid Patient ID");
return;
}
int patWeight;
if (!int.TryParse(txtWeight.Text, out patWeight))
{
MessageBox.Show("Please enter a valid Patient Weight");
return;
}
Patient = new Patient(txtName.Text, patId, patWeight);
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

What I have tried:

And here is my add patient form.

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 PatientRecords
{
    public partial class Add_Patient_Info : Form
    {
        public static Intake intake = null;
        public Intake GetNewPatient ()
        {
            this.ShowDialog();
            return intake;
        }

        public Add_Patient_Info()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

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

            }
            catch (OverflowException)
            {

            }
        }
    }

推荐答案

你有什么应该工作 - 但我建议你不要使用Convert.ToXXX而是使用TryParse:

What you have there should work - but I'd suggest that you don't use Convert.ToXXX and use TryParse instead:
private void btnCalculate_Click(object sender, EventArgs e)
    {
    int patId;
    if (!int.TryParse(txtPatientId.Text, out patId))
       {
       MessageBox.Show("Please enter a valid Patient ID");
       return;
       }
    int patWeight;
    if (!int.TryParse(txtWeight.Text, out patWeight))
       {
       MessageBox.Show("Please enter a valid Patient Weight");
       return;
       }
    Intake intake = new Intake(txtName.Text, patId, patWeight);
    txtProtein.Text = intake.GetProteinIntake().ToString();
    }



您可能会发现它有效。

并且请:永远不要吞下异常 - 它使调试非常困难代码,当你这样做,因为你不知道什么时候失败。将其报告给用户,或将其记录下来以供日后分析 - 但不要只是吞下它并继续前进。



如果你打算将乘法数乘以0.86 ,我强烈建议你使用双值而不是整数。



我知道我认为不同的类级变量,公共和私有,以及



我试图将添加患者表格中的信息带到一个列表框(我工作时暂不能发布)。 / b>



好​​的 - 所以看看Matt在解决方案2中的建议 - 这都是好主意。

如果要添加新的患者到一个不同形式的列表框,然后你想要通过一个属性将新病人作为一个类实例返回,让前面的表格(我假设有列表框)处理显示它。

这不是太糟糕 - 验证上面的文本框,并使用类级别Patient类返回新实例 - 删除进入上课,然后创建一个患者。

然后


You may find that works.
And please: never "swallow" exceptions - it makes it very hard to debug code when you do that as you don't know when something fails. Report it to the user, or log it for later analysis - but never just swallow it and move on.

And if you are going to work with multiplying numbers by 0.86, I'd strongly suggest you use double values instead of integers.

"I know of the different class level variables I believe, public and private, and

I am trying to take the information from the add patient form to a list box (which I can not post at the moment as I am at work)."


Ok - so look at what Matt suggests in Solution 2 - it's all good ideas.
If you are adding the new patient to a list box in a different form, then you either want to return the new patient as a class instance via a property, and let the previous form (which I assume has the list box) deal with displaying it.
That's not too bad - verify the text boxes as above, and use a class level Patient class to return the new instance - remove the "Intake" class and create a Patient one instead.
then

public Patient { get; private set; }



并且您的点击进行验证然后:


and your click does the validation and then:

Patient = new Patient(txtName.Text, patId, patWeight);



并关闭表单,返回DialogResult.OK



然后父表格收集信息:


And closes the form, returning DialogResult.OK

The parent form then collects the info:

frmNewPatient fnp = new frmNewPatient();
if (fnp.ShowDialog() == DialogResult.OK)
   {
   Patient theNewGuy = fnp.Patient;
   ... display the new patient in the Listbox ...
   }

这样,你传递了你需要的信息,而不是创建一个类来处理一些相当无关的病人信息!



有意义吗?

That way, you are passing around the info you need, instead of creating a class to deal with a tiny bit of fairly irrelevant patient info!

Make any sense?


有几个问题:

首先,你丢弃了你在btnCalculate_Click方法中创建的Intake。试试(包括上面的Original Griff的建议):

There's several things wrong:
First you're throwing away the Intake you create in the btnCalculate_Click method. Try (including the suggestions of Original Griff, above):
private void btnCalculate_Click(object sender, EventArgs e)
{
int patId;
if (!int.TryParse(txtPatientId.Text, out patId))
   {
   MessageBox.Show("Please enter a valid Patient ID");
   return;
   }
int patWeight;
if (!int.TryParse(txtWeight.Text, out patWeight))
   {
   MessageBox.Show("Please enter a valid Patient Weight");
   return;
   }
intake = new Intake(txtName.Text, patId, patWeight);
txtProtein.Text = intake.GetProteinIntake().ToString();
}



具体来说,不要在方法中创建局部变量 intake ,使用类字段。

接下来,该类字段摄入量可能不应该是 static 。它应该特定于表格的实例。



此外,您还可以进行其他简化。

首先,在Intake类,您不需要为3个属性显式声明支持字段。您可以让编译器为您完成。


Specifically, don't create a local variable intake in the method, use the class field.
Next, that class field intake probably should not be static. It should be specific to the instance of the form.

In addition, there's other simplification that you can do.
First, in the Intake class, you don't need to explicitly declare the "backing fields" for the 3 properties. You can let the compiler do it for you.

public class Intake
{
    private string name;
    private int patientId;
    private int weight;
// ...
    public string Name { get; set; }
    public int PatientId { get; set; }
    public int Weight { get; set; }



然后在 GetProteinIntake 方法中,更改为使用 Weight 而不是权重

此外,该方法可以轻松更改为只获取属性,如:


Then in the GetProteinIntake method, change to use Weight instead of weight.
Also, that method could easily be changed to a get-only property, like:

public double ProteinIntake { get { return Weight * .86; } }



我将它重命名为一个属性。

然后它将被用作:


I've renamed it to look like a property.
Then it would be used like:

txtProtein.Text = intake.ProteinIntake.ToString();


这篇关于如何在私有类中执行等式,并将其传递以添加到客户表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆