Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

C# dynamically increase / decrease a number

I am writing an app and I'm trying to perform a test.

    public class PatientDiagnostics
    {
        public int PersonID { get; set; }
        public int HeartRate { get; set; }
    }

Open in new window


I would like to dynamically change the heart rate.  Start off with HeartRate = 20.  Then increase and decrease it +/- 2, 3.
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Could you please tell more about your task? I'd say it is not clear.

May be you need just a "for" loop or similar...
Avatar of CipherIS

ASKER

When I add a person I would like to create it dynamically to the ressults are like:

(1)  PersonID = 1, HeartRate = 20
(2)  PersonID = 1, HeartRate = 18
(3)  PersonID = 1, HeartRate = 22
(4)  PersonID = 1, HeartRate = 18
(5)  PersonID = 1, HeartRate = 19
Still not 100% clear but try this.

I have a windows app with 1 button.

See the code below

namespace Diagnostics
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int personID = 5;
            int heartRateIncrement = 2;
            List<PatientDiagnostics> allPatientDiagnostics = new List<PatientDiagnostics>();
            for( int nextHeartRate = 20; nextHeartRate<=200; nextHeartRate = nextHeartRate+heartRateIncrement)
            {
                PatientDiagnostics pd = new PatientDiagnostics(personID, nextHeartRate);
                allPatientDiagnostics.Add(pd);
            }

            // print list content:
            foreach (PatientDiagnostics pd in allPatientDiagnostics)
            {
                System.Diagnostics.Debug.WriteLine(pd.toString());
            }
        }
    }

    public class PatientDiagnostics
    {
        private int _personID ;
        private int _heartRate;
        public int PersonID { get {return _personID;} }
        public int HeartRate { get; set; }

        public PatientDiagnostics(int personID, int heartRate)
        {
            _personID = personID;
            HeartRate = heartRate;
        }

        public string toString()
        {
            return "PersonID = " + this._personID + ", HearRate = " + HeartRate;
        }

    }
}

Open in new window


Output is like:
PersonID = 5, HearRate = 20
PersonID = 5, HearRate = 22
PersonID = 5, HearRate = 24
PersonID = 5, HearRate = 26
PersonID = 5, HearRate = 28
etc.
That looks like it would work.  What I did was set the increment with a mod (%).  This way it won't go to high for this fake data.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.