C#检查日期时间不工作 [英] c# check for datetime not working

查看:111
本文介绍了C#检查日期时间不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建C#中我自己的DateTime类以及由于某种原因,我的检查不工作。当我运行该程序,并输入一个日期,它总是停止,达到这是对天的方法,并说ArgumentOutOfException。反正有没有解决这个问题,使我的支票被称为价值的最后一行的工作?

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;

命名空间日期
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            Console.WriteLine(\ t \ t \ t \ t \ t \ tNextDate程序\ n \ t \ t \ t \ t \ t -------------------- -----------------);

            Console.WriteLine(请为DD / MM / YYYY输入日期);
            INT天;
            INT个月;
            INT年;

            字符串[]读=到Console.ReadLine()斯普利特('/')。
            天= int.Parse(读[0]);
            一个月= int.Parse(读[1]);
            年= int.Parse(读[2]);

            日期日期=新的日期(日,月,年);
            Console.WriteLine({0} / {1} / {2},date.Day,date.Month,date.Year);
            到Console.ReadLine();
        }

        类Date
        {
            私人诠释_month; // 1-12
            私人诠释_day; // 1-31取决于一个月
            私人诠释_year;

            公开日期(INT日,INT月,年整型)
            {
                天=天;
                月=月;
                年=年;
            }

            公众诠释新年
            {
                {返回_year; }
                组
                {
                    如果(值GT = 1820&功放;&放大器;值小于= 2020)
                        _year =价值;
                    其他
                        抛出新ArgumentOutOfRangeException(年,价值的一年超出范围);
                }
            }

            公众诠释月份
            {
                {返回_month; }
                组
                {
                    如果(值大于0&安培;&安培;值小于; = 12)
                        _month =价值;
                    其他
                        抛出新ArgumentOutOfRangeException(月,值,月必须是1-12);
                }
            }

            公众诠释日
            {
                {返回_day; }
                组
                {

                    INT []天= {0,31,28,31,30,31,30,31,31,30,31,30,31};

                    如果(值大于0和放大器;&放大器;值小于=天[_month])
                        _day =价值;

                    否则,如果(_month == 2及和放大器;值== 29安培​​;&安培;
                        _year%400 == 0 || (_year%4 == 0安培;&安培;!_year%100 = 0))
                        _day =价值;
                    其他
                    抛出新ArgumentOutOfRangeException(天,价值,天超出范围);



                }

            }
        }
    }
}
 

解决方案

在你的构造:

 公开日期(INT日,INT月,年整型){
  天=天;
  月=月;
  年=年;
}
 

您第一次分配的属性。此时将被默认初始化 - 零 - 但是在制定者承担<$ C $支票C>月已设置。因此,在一个月价值的日子将是0。

您需要重新思考如何做到这一点。也许更好的是不使用自动属性,您可以设置字段,然后确认日期作为一个整体。

预订setter和验证那里到外部接口

更妙的是使它成为imutable值类型。

更妙的是将不会重新发明轮子(除非你有非常充分的理由)。

I am creating my own DateTime class within C# and for some reason my checks are not working. When I run the program and enter a date it always stops and reaches the last line which is on the "Day" method and says "ArgumentOutOfException. Is there anyway to solve this issue and to make my checks called "Value" working?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace date
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\t\t\t\t\t\tNextDate Application\n\t\t\t\t\t-------------------------------------");

            Console.WriteLine("please enter date as dd/MM/yyyy");
            int day;
            int month;
            int year;

            string[] read = Console.ReadLine().Split('/');
            day = int.Parse(read[0]);
            month = int.Parse(read[1]);
            year = int.Parse(read[2]);

            Date date = new Date(day, month, year);
            Console.WriteLine("{0}/{1}/{2}", date.Day, date.Month, date.Year);
            Console.ReadLine();
        }

        class Date
        {
            private int _month; // 1-12
            private int _day; // 1-31 depending on month
            private int _year;

            public Date(int day, int month, int year)
            {
                Day = day;
                Month = month;
                Year = year;
            }

            public int Year
            {
                get { return _year; }
                set
                {
                    if (value >= 1820 && value <= 2020)
                        _year = value;
                    else
                        throw new ArgumentOutOfRangeException("year", value, "year out of range");
                }
            }

            public int Month
            {
                get { return _month; }
                set
                {
                    if (value > 0 && value <= 12)
                        _month = value;
                    else
                        throw new ArgumentOutOfRangeException("Month", value, "Month must be 1-12");
                }
            }

            public int Day
            {
                get { return _day; }
                set
                {

                    int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

                    if (value > 0 && value <= days[_month])
                        _day = value;

                    else if (_month == 2 && value == 29 &&
                        _year % 400 == 0 || (_year % 4 == 0 && _year % 100 != 0))
                        _day = value;
                    else
                    throw new ArgumentOutOfRangeException("Day", value, "Day is out of range");



                }

            }
        }
    }
}

解决方案

In your constructor:

public Date(int day, int month, int year) {
  Day = day;
  Month = month;
  Year = year;
}

you assigned the Day property first. At this point Month will be default initialised – to zero – but the checks in the Day setter assume Month has been set. So the days in the month value will be 0.

You need to rethink how you do this. Probably better is to not use automatic properties so you can set the fields and then validate the date as a whole.

Reserve setters and validation there to the external interface.

Even better would be to make it an imutable value type.

Better still would be to not re-invent the wheel (unless you have very good reason).

这篇关于C#检查日期时间不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆