简体   繁体   中英

C# simple constructor overload

I am working on a very simple overloaded constructor exercise, but am running into a no overload taking for parameters found. I thought I have done this correctly, but maybe I am doing something wrong?

   namespace A3_Date_StudentWork
    {
        class MyDate
        {      


            private int newMonth;
            private int newDay;
            private int currentMonth;
            private int currentDay;

            public MyDate(int month, int day)
            {
               newMonth = month;
               newDay = day;
            }

            public MyDate(int month, int day, int cmonth, int cday)
            {
                newMonth = month;
                newDay = day;
                currentMonth = cmonth;
                currentDay = cday;
            }
        }
    }

Here is what my new objects look like from main

    MyDate today = new MyDate(todayMonth, todayDay);

    MyDate bday = new MyDate(bdayMonth, bdayDay);

    MyDate combine = new MyDate(bdayMonth, bdayDay, todayMonth, todayDay);

It is pretty simple program, all of my variables are int. My entire Main:

    static void Main(string[] args)
    {

        int todayMonth = 0;
        int todayDay = 0;
        int bdayMonth = 0;
        int bdayDay = 0;


        Console.WriteLine("What is today's month?");
        Int32.TryParse(Console.ReadLine(), out todayMonth);
       // Console.WriteLine(todayMonth);

        Console.WriteLine("What is today's date?");
        Int32.TryParse(Console.ReadLine(), out todayDay);
       // Console.WriteLine(todayDay);

        Console.WriteLine("What is your birth month?");
        Int32.TryParse(Console.ReadLine(), out bdayMonth);
       // Console.WriteLine(bdayMonth);

        Console.WriteLine("What is your birth date?");
        Int32.TryParse(Console.ReadLine(), out bdayDay);
       // Console.WriteLine(bdayDay);

        MyDate today = new MyDate(todayMonth, todayDay);
        MyDate bday = new MyDate(bdayMonth, bdayDay);
        MyDate combine = new MyDate(bdayMonth, bdayDay, todayMonth, todayDay);

And here is the base class with the constructors

    {
class MyDate
{      


    private int newMonth;
    private int newDay;
    private int currentMonth;
    private int currentDay;

    public MyDate(int month, int day)
    {
       newMonth = month;
       newDay = day;
    }

    public MyDate(int month, int day, int cmonth, int cday)
    {
        newMonth = month;
        newDay = day;
        currentMonth = cmonth;
        currentDay = cday;
    }

The base class is literally the class MyDate

Just a piece of advice here in terms of technical. When you have multiple constructors you should from one constructor call the other one if possible by passing the values. This is also known as 'Constructor Chaining'.

Here's an example based on your code:

class MyDate
{
    private int newMonth;
    private int newDay;
    private int currentMonth;
    private int currentDay;

    public MyDate(int month, int day)
       : this(month, day, 0, 0)
    { }

    public MyDate(int month, int day, int cmonth, int cday)
    {
        newMonth = month;
        newDay = day;
        currentMonth = cmonth;
        currentDay = cday;
    }
}

In this simple case, when you call the constructor with 2 parameters (month and day) the other parameters will be 0 by default. You can also make parameters nullable so you can assign a null value to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM