Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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);
       }
    }
}


Edit: 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.

C#
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)
            {

            }
        }
    }
Posted
Updated 25-Jul-16 17:51pm
v3

What you have there should work - but I'd suggest that you don't use Convert.ToXXX and use TryParse instead:
C#
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();
    }

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
C#
public Patient { get; private set; }

and your click does the validation and then:
C#
Patient = new Patient(txtName.Text, patId, patWeight);

And closes the form, returning DialogResult.OK

The parent form then collects the info:
C#
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?
 
Share this answer
 
v3
Comments
Member 12652642 25-Jul-16 11:46am    
Forgive my ignorance, but will that be able to pass the protein intake to another form I made that I add and delete records? I ask because there is no specific variable for it.
OriginalGriff 25-Jul-16 11:58am    
That's complicated, because I don't know exactly what you are doing.
The problem is that you create a new instance of the Intake class in your Calculate method, but you don't "keep" it anywhere - so it is discarded when the method ends. You do keep the protein intake value to calculated (you store it in your TextBox) so you could use that, but it depends on what you are trying to do how you should really approach this.
Clearly you haven't been doing this long, but I don't know how much you have been taught - so it's really difficult to explain without adding to your confusion by introducing new concepts! :laugh:
So...
Do you know what an instance is? A class level variable? How do you show the user the other form, and where do you show it from?
Member 12652642 25-Jul-16 13:47pm    
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).
OriginalGriff 25-Jul-16 14:11pm    
Answer updated.
Member 12652642 25-Jul-16 14:48pm    
Everything makes sense except creating the new class. I agree that the intake class was not necessary, but then you said to make a new class named Patient? Maybe I don't understand how class level variables work.

I assume the fnp is akin to the file pointer in c? I have some experience with C, barely any in C# as you have noticed!
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):
C#
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();
}

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.
C#
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; }

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:
C#
public double ProteinIntake { get { return Weight * .86; } }

I've renamed it to look like a property.
Then it would be used like:
C#
txtProtein.Text = intake.ProteinIntake.ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900