Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

A brief summary: I am very new to C# and this is my first time programming in this language. I have more experience with scripting languages. For a college assignment we must make an age calculator which calculates the age in days of the user, based on combo box input.

My question is, how (and where) do I implement leap year control within my code? I am aware of DateTime.IsLeapYear, however I am very unfamiliar with the syntax and do not know where to start with it.

I have added my code below so you can see my progress so far. I am working in Visual Studio 2017.

P.S. I must also implement a try/catch block somewhere, but as I said above I do not know where to start with it. Where would this go?

What I have tried:

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 NumberOfDaysOld
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //=========================================================
            // DISPLAYS MONTHS OF THE YEAR IN THE MONTH COMBOBOX
            //=========================================================
            string[] monthArray = new string[12];
            monthArray[0] = "January";
            monthArray[1] = "February";
            monthArray[2] = "March";
            monthArray[3] = "April";
            monthArray[4] = "May";
            monthArray[5] = "June";
            monthArray[6] = "July";
            monthArray[7] = "August";
            monthArray[8] = "September";
            monthArray[9] = "October";
            monthArray[10] = "November";
            monthArray[11] = "December";

            for (int index = 0; index < monthArray.Length; index++)
            {
                comboBoxMonth.Items.Add(monthArray[index]);
            }

            //=========================================================
            // DISPLAYS YEARS 1900 TO CURRENT YEAR IN THE YEAR COMBOBOX
            //=========================================================

            for (int i = 1900; i <= DateTime.Now.Year; i++)
            {
                comboBoxYear.Items.Add(i.ToString());
            }

            //=========================================================
            // DISPLAYS DATES OF THE MONTH IN THE DAYS COMBOBOX
            //=========================================================

            for (int i = 1; i <= 31; i++)
            {
                comboBoxDay.Items.Add(i.ToString());
            }

        }

        private void timerDateTime_Tick(object sender, EventArgs e)
        {
            //=========================================================
            // DISPLAYS THE DATE AND TIME IN REALTIME
            //=========================================================

            labelDate.Text = DateTime.Now.ToShortDateString();
            labelTime.Text = DateTime.Now.ToLongTimeString();
        }

        private void comboBoxDay_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBoxMonth_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        private void buttonCalculate_Click(object sender, EventArgs e)
        {
            DateTime dob = new DateTime(Convert.ToInt32(comboBoxYear.SelectedItem), comboBoxMonth.SelectedIndex + 1, Convert.ToInt32(comboBoxDay.SelectedItem));
            TimeSpan diff = DateTime.Now - dob;
            int days = (int)diff.TotalDays;
            MessageBox.Show(textBoxForename.Text + " is days old " + days.ToString());
        }

        }
    }
}
Posted
Comments
[no name] 28-Mar-17 20:12pm    
https://msdn.microsoft.com/en-us/library/system.datetime.isleapyear(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/0yd65esw.aspx
PIEBALDconsult 28-Mar-17 22:00pm    
Why do it the hard way?
System.DateTime dt0 = new System.DateTime ( 2016 , 02 , 01 ) ;
System.DateTime dt1 = new System.DateTime ( 2016 , 03 , 01 ) ;
System.TimeSpan ts = dt1 - dt0 ;
System.Console.WriteLine ( ts.TotalDays ) ;
Richard Deeming 30-Mar-17 15:20pm    
I guess you just want to check whether the selected day is less than or equal to the number of days in the selected month[^]?

No need for any special leap-year code; the DateTime type takes care of that for you.
Nick_3141592654 14-Apr-17 17:55pm    
Regarding the try-catch that you asked about. This is apparently a college assignment, the purpose of which is presumably for you to: (a) learn something and (b) demonstrate that you've learnt it.

Given those aims, why don't you invest 15 minutes READING about try-catch to understand what it is for, then you will know where it's appropriate to use it.

If somebody just says "oh you can put one here.." you'll be none the wiser.

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