Z
Z
Zhenia Bel2022-01-10 17:27:59
C++ / C#
Zhenia Bel, 2022-01-10 17:27:59

How to override ToString for my Date class?

I have my own Date class, but when I want to output values ​​to another class, it displays the Project Name.Data, I read on the Internet that I need to override ToString but I don’t understand how to do it

Here is the Date class code:

internal class Date
    {
        protected int Year; 
        protected int Month;
        protected int Day;
        protected int Hours;
        protected int Minutes;

        
        public Date()
        {
            Year = 2022;
            Month = 1;
            Day = 11;
            Hours = 12;
            Minutes = 20;
        }
         
        
        public int GetYear()
        {
          return Year;
        }
        public int GetMonth()
        {
            return Month;
        }
        public int GetDay()
        {
            return Day;
        }
        public int GetHours()
        {
            return Hours;
        }
        public int GetMinutes()
        {
            return Minutes;
        }

        public void SetYear(int year)
        {
            Year = year;
        }
        public void SetMonth(int month)
        {
            if(Month > 0 && Month <= 12)
            {
                Month = month;
            }
        }
        public void SetDay(int day)
        {
            if(Day > 0 && Day <= 31)
            {
                Day = day;
            }
        }
        public void SetHours(int hours)
        {
            if(Hours > 0 && Hours <= 24)
            {
                Hours = hours;
            }
        }
        public void SetMinutes(int minutes)
        {
            if(Minutes > 0 && Minutes <= 60)
            {
                Minutes = minutes;
            }
        }
    }

Here is another class in which you need to display values ​​from Date:
internal class Airplane
    {
        protected string StartCity;
        protected string FinishCity;
        protected Date StartDate;
        protected Date FinishDate;

       
        public Airplane()
        {
            StartCity = "London";
            FinishCity = "Lviv";
            StartDate = new Date();
            FinishDate = new Date();
        }
        
        public string GetStartCity()
        {
            return StartCity;
        }
        public string GetFinishCity()
        {
            return FinishCity;
        }

        public Date GetStartDate()
        {
            return StartDate;
        }
        
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2022-01-10
@defild

public override string ToString()
{
   return "Я не читаю документацию, хотя ссылку получил в предыдущем вопросе";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question