2011-10-22 159 views
0

我已经看到了此错误消息问题在计算器中,但它们都不是日期时间或日期类型为了使用只有日期类型I已经创建了一类日期类型,并在日期类中为它写了一些重载。我的约会类是无法投入'Common.Date'类型的对象来键入'System.IConvertible C#

using System; 
namespace Common 
{ 
    public class Date 
    { 
     private DateTime _d1; 

     public Date(DateTime dateTime) 
     { 
      _d1 = dateTime; 
     } 

     public static bool operator <(Date date1, Date date2) 
     { 
      bool flag = false; 


      //Now, get the original DateTime Type of C# 
      DateTime firstDate = Convert.ToDateTime(date1); 
      DateTime secondDate = Convert.ToDateTime(date2); 

      //Now compare the two DateTime variables and assign the flag to true 
      //if the first date is smaller than the second date 
      int result = DateTime.Compare(firstDate, secondDate); 
      if (result < 0) 
      { 
       flag = true; 
      } 
      return flag; 
     } 

     public static bool operator >(Date date1, Date date2) 
     { 
      bool flag = false; 

      //Now, get the original DateTime Type of C# 
      DateTime firstDate = Convert.ToDateTime(date1); 
      DateTime secondDate = Convert.ToDateTime(date2); 

      //Now compare the two DateTime variables and assign the flag to true 
      //if the first date is Greater than the second date 
      int result = DateTime.Compare(firstDate, secondDate); 
      if (result > 0) 
      { 
       flag = true; 
      } 
      return flag; 
     } 

     public static bool operator <=(Date date1, Date date2) 
     { 
      bool flag = false; 

      //Now, get the original DateTime Type of C# 
      DateTime firstDate = Convert.ToDateTime(date1); 
      DateTime secondDate = Convert.ToDateTime(date2); 

      //Now compare the two DateTime variables and assign the flag to true 
      //if the first date is Greater than the second date 
      int result = DateTime.Compare(firstDate, secondDate); 
      if (result <= 0) 
      { 
       flag = true; 
      } 

      return flag; 
     } 

     public static bool operator >=(Date date1, Date date2) 
     { 
      bool flag = false; 

      //Now, get the original DateTime Type of C# 
      DateTime firstDate = Convert.ToDateTime(date1); 
      DateTime secondDate = Convert.ToDateTime(date2); 

      //Now compare the two DateTime variables and assign the flag to true 
      //if the first date is Greater than the second date 
      int result = DateTime.Compare(firstDate, secondDate); 
      if (result >= 0) 
      { 
       flag = true; 
      } 
      return flag; 
     } 

     public static bool operator ==(Date date1, Date date2) 
     { 
      bool flag = false; 

      //Now, get the original DateTime Type of C# 
      DateTime firstDate = Convert.ToDateTime(date1); 
      DateTime secondDate = Convert.ToDateTime(date2); 

      //Now compare the two DateTime variables and assign the flag to true 
      //if the first date is Greater than the second date 
      int result = DateTime.Compare(firstDate, secondDate); 
      if (result == 0) 
      { 
       flag = true; 
      } 
      return flag; 
     } 

     public static bool operator !=(Date date1, Date date2) 
     { 
      bool flag = false; 

      //Now, get the original DateTime Type of C# 
      DateTime firstDate = Convert.ToDateTime(date1); 
      DateTime secondDate = Convert.ToDateTime(date2); 

      //Now compare the two DateTime variables and assign the flag to true 
      //if the first date is Greater than the second date 
      int result = DateTime.Compare(firstDate, secondDate); 
      if (result != 0) 
      { 
       flag = true; 
      } 
      return flag; 
     } 
    }//end of class Date 
}//End of namespace 

,但问题是当我试图在页面背后的给我这个错误我的代码使用 - 无法投类型的对象Common.Date为键入“System.IConvertible

我使用它的代码像 Date purchaseDate = new Date(item.PurchaseDate); date submissionSate = new Date(item.SubmissionDate);

    if (purchaseDate>submissionSate) 
        { 
         //to do 
        } 

在这里的项目对象而purchaseDate和submision日期是日期时间属性和错误的,如果行 谁能给我提供任何suggesion?这个问题的可能解决方案是什么?

回答

1

在你>操作符重载,你有

DateTime firstDate = Convert.ToDateTime(date1); 
DateTime secondDate = Convert.ToDateTime(date2); 

并没有的Convert.ToDateTime重载需要你Date对象,所以你打电话Convert.ToDateTime(object),这需要object实施IConvertible

您可以实施IConvertible,或只是比较_d1的值作为@ChaosPandion提到。

+0

无法正确理解,请您进一步解释一下?这对你真好。 – Pankouri

+0

如果您查看['Convert.ToDateTime']的重载(http://msdn.microsoft.com/zh-cn/library/dw6yd06a.aspx),您会看到唯一一个可以是被称为'Convert.ToDateTime(object)',并且为了使Convert.ToDateTime工作,'object'必须实现'IConvertible'接口(参见文档)。由于你的Date类没有实现'IConvertible','Convert.ToDateTime(date1)'抛出异常。 –

+0

感谢问题解决。 – Pankouri

2

您可以直接访问Date的字段。虽然我质疑这个Date对象的用处。

public static bool operator <(Date date1, Date date2) 
{ 
    return date1 != null && date2 != null && date1._d1 < date2._d1 
} 
+0

其实我写了日期类来学习c#中的操作符重载,然后如何使用它们,并且确定代码的--date1._d1 Pankouri

+0

@Pankouri - 所有私人会员都可以在Date日期范围内访问。 – ChaosPandion

相关问题