0

这是我收到消息的 C# 类的更新版本:

检测到无法访问的代码:DateTime.Now.Ticks;

其他一切似乎都很好。我的错误在哪里?

using System;
using System.Globalization;

namespace domain
{
    public class Request : Unique
    {
        private DateTime dateBooked_Renamed;

        private DateTime dateSignedIn ;

        private DateTime dateSignedOut ;

        private bool arrived_Renamed = false;

        private bool paid_Renamed = false;

        private Hotel hotel_Renamed;

        private Employee employee_Renamed;

        private int numberOfNights;

        public Request(Hotel hotel, Employee employee, string dateBooked, string dateArrived, int numberOfNights)
            : base()
        {
            this.hotel_Renamed = hotel;
            this.employee_Renamed = employee;
            this.numberOfNights = numberOfNights;
            try
            {
                DateTime.ParseExact(dateBooked, "dd/MM/yyyy", CultureInfo.InvariantCulture);

                this.dateBooked_Renamed = DateTime.ParseExact(dateBooked, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                this.dateSignedIn = DateTime.ParseExact(dateArrived, "dd/MM/yyyy", CultureInfo.InvariantCulture);
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
            }
        }

        public virtual bool checkout(double money, string date)
        {
            try
            {
                dateCheckedOut = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);

            }
            catch (FormatException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
            }
            if (money >= price())
            {
                paid = true;
            }
            else
            {
                paid = false;
            }
            return paid_Renamed;
        }

        public virtual int days()
        {
            long tNow = dateSignedOut != null ? dateSignedOut.Ticks : DateTime.Now.Ticks; 
            long tThen = arrived ? dateArrived.Ticks : tNow;
            long diff = tNow - tThen;

            long secondInMillis = 1000;
            long minuteInMillis = secondInMillis * 60;
            long hourInMillis = minuteInMillis * 60;
            long dayInMillis = hourInMillis * 24;
            long yearInMillis = dayInMillis * 365;

            diff = diff % yearInMillis;
            long elapsedDays = diff / dayInMillis;

            return (int)elapsedDays;
        }

        public virtual Hotel hotel
        {
            get
            {
                return hotel_Renamed;
            }
            set
            {
                this.hotel_Renamed = value;
            }
        }

        public virtual DateTime dateArrived
        {
            get
            {
                return dateSignedIn;
            }
            set
            {
                this.dateSignedIn = value;
            }
        }

        public virtual DateTime dateBooked
        {
            get
            {
                return dateBooked_Renamed;
            }
            set
            {
                this.dateBooked_Renamed = value;
            }
        }

        public virtual DateTime dateCheckedOut
        {
            get
            {
                return dateSignedOut;
            }
            set
            {
                this.dateSignedOut = value;
            }
        }

        public virtual Employee employee
        {
            get
            {
                return employee_Renamed;
            }
            set
            {
                this.employee_Renamed = value;
            }
        }

        public virtual bool arrived
        {
            get
            {
                return arrived_Renamed;
            }
            set
            {
                this.arrived_Renamed = value;
            }
        }

        public virtual bool paid
        {
            get
            {
                return paid_Renamed;
            }
            set
            {
                this.paid_Renamed = value;
            }
        }

        public virtual double price()
        {
            // The total cost of the stay...

            double price = days() * employee_Renamed.type.price;
            if (hotel_Renamed.premium)
            {
                return price - (0.2 * price);
            }
            else
            {
                return price;
            }
        }



        public virtual bool hasArrivalDate(string date)
        {
            try
            {
                DateTime d = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                Console.WriteLine("Comparing " + d + " with " + dateSignedIn + " = " + dateSignedIn.ToString("dd/MM/yyyy").Equals(d.ToString("dd/MM/yyyy")));
                return dateSignedIn.ToString().Equals(d.ToString());
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
                return false;
            }
        }
    }
}
4

2 回答 2

0

如果你的日期字符串是标准格式,你可以使用DateTime.Parse(date)
如果不是,你可以使用DateTime.ParseExact(date,"your custom format",CultureInfo.InvariantCulture)

作为您的代码示例

public virtual bool hasArrivalDate(string date)
        {
            try
            {
                DateTime d = DateTime.ParseExact(date,"dd/MM/yyyy",CultureInfo.InvariantCulture);
                Console.WriteLine("Comparing " + d.ToString("dd/MM/yyyy") + " with " + dateSignedIn.ToString("dd/MM/yyyy") + " = " + dateSignedIn.ToString("dd/MM/yyyy").Equals(d.ToString("dd/MM/yyyy")));
                return dateSignedIn.ToString().Equals(d.ToString());
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
                return false;
            }
        }

所以,不需要 DateFormat 的实例

于 2012-05-11T15:47:09.357 回答
0

在您的代码中, dateSignedOut 变量是 a DateTime,它永远不能为空。

因此,在这一行:

long tNow = dateSignedOut != null ? dateSignedOut.Ticks : DateTime.Now.Ticks;

dateSignedOut 永远不会为空,因此永远不会到达您声明的“else”部分。

如果您希望 dateSignedOut 可以为空,请将其设为Nullable<DateTime>(或DateTime?)。

顺便说一句,请格式化您的问题,以便它们更具可读性。这个真的有被关闭的危险。更好的问题会带来更好的答案。

根据评论更新

您可能应该尝试更多地了解通常如何用 C# 编写属性,但这超出了这个问题的范围。要使用您已有的代码:

private DateTime? dateSignedOut;  // Notice the ? to make it Nullable
long tNow = dateSignedOut.HasValue ? dateSignedOut.Ticks : DateTime.Now.Ticks;

查看一些MSDN 文档或 Google Nullable 类型。

于 2012-05-11T17:16:26.083 回答