What do I use for args when calling constructor in C#?

I have an assignment in my intro class where I need to prompt the user for first name, last name, birth year, and the current year. The program needs to output the information back to the user along with calculated age, maximum heartrate (220 - age), and target heartrate range (50% to 85%). My class should have a constructor that receives the inputted data as parameters. Each attribute should have get/sets and there should be properties to calculate age, max heart rate, and the target range. This is what I have so far. I think I have done what it asks but I'm not sure what args to use when calling the constructor. Or maybe I am misunderstanding a concept here.

Thanks in advance for any advice.

using System;

namespace Assignment2
{
    public class HeartRates
    {
        private string firstname;
        private string lastname;
        private int birthyear;
        private int currentyear;
        private int age;
        private double maxrate;
        private double mintarget;
        private double maxtarget;

        public string Firstname
        {
            get { return firstname; }
            set { firstname = value; }
        }

        public string Lastname
        {
            get { return lastname; }
            set { lastname = value; }
        }

        public int Birthyear
        {
            get { return birthyear; }
            set { birthyear = value; }
        }

        public int Currentyear
        {
            get { return currentyear; }
            set { currentyear = value; }

        }

        public int Age
        {
            get { return currentyear - birthyear; }
            set { age = currentyear - birthyear; }
        }

        public double MaxRate
        {
            get { return 220 - Age; }
            set { maxrate = value; }
        }

        public double MinTarget
        {
            get { return MaxRate * .5; }
            set { mintarget = value; }
        }
        public double MaxTarget
        {
            get { return MaxRate * .85; }
            set { maxtarget = maxrate * .85; }
        }
        public HeartRates(string firstname, string lastname, int birthyear, int currentyear)
        {
            Console.WriteLine("Please enter your first name:");
            this.firstname = Console.ReadLine();
            Console.WriteLine("Please enter your last name:");
            this.lastname = Console.ReadLine();
            Console.WriteLine("Please enter your birth year:");
            this.birthyear = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter the current year:");
            this.currentyear = Convert.ToInt32(Console.ReadLine());
        }
    }

    public class Program
    {
        public static void Main()
        {
            HeartRates newheartrates = new HeartRates();

            Console.WriteLine("Your name is {0} {1}, you were born in {2}, the current year is {3}", newheartrates.Firstname, newheartrates.Lastname, newheartrates.Birthyear, newheartrates.Currentyear);
            Console.WriteLine("Your age is {0}", newheartrates.Age);
            Console.WriteLine("Your maximum heart rate is {0}", newheartrates.MaxRate);
            Console.WriteLine("Your target heart rate range is from {0} to {1}.", newheartrates.MinTarget, newheartrates.MaxTarget);
        }
    }
}

Answers


You are on the right track, but I think you are little confused.

You should move this part

        Console.WriteLine("Please enter your first name:");
        this.firstname = Console.ReadLine();
        Console.WriteLine("Please enter your last name:");
        this.lastname = Console.ReadLine();
        Console.WriteLine("Please enter your birth year:");
        this.birthyear = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Please enter the current year:");
        this.currentyear = Convert.ToInt32(Console.ReadLine());

To your main function.

Then use the constructor you created instead.

HeartRates newheartrates = new HeartRates(firstname, lastname, birthyear, currentyear);

What you have could kind of work, but 1) you are not calling the constructor you created, you are calling a constructor with no parameter. Typically would don't want user input in your object constructors. It makes more sense to do it in the main function and keep your input/output together.

Hope this helps, welcome to Stackoverflow.


First get user inputs (inside of your main method,not in the constructor):

Console.WriteLine("Please enter your first name:");
string firstname = Console.ReadLine();
Console.WriteLine("Please enter your last name:");
string lastname = Console.ReadLine();
Console.WriteLine("Please enter your birth year:");
int birthyear = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the current year:");
int = currentyear = Convert.ToInt32(Console.ReadLine());

Then call your constructor:

HeartRates newheartrates = new HeartRates(firstname,lastname,birthyear,currentyear);

And change your constructor:

public HeartRates(string firstname, string lastname, int birthyear, int currentyear)
{
     this.firstname = firstname;
     this.lastname = lastname;
     this.birthyear = birthyear;
     this.currentyear = currentyear;
}

Need Your Help

MySQL-python installation - wrong architecture - MacOS

python mysql django macos python-2.7

yesterday I've installed new version of python 2.7.5 via installer on MacOS

Best settings for Android camera to capture LCD?

android android-camera

I notice the Android camera adjusts its settings according to light conditions.