2011-10-22 91 views
0

このエラーメッセージの問題はstackoverflowで見ましたが、どちらも日付型I日付型のクラスを作成し、日付クラスにそのためのいくつかのオーバーロードを書いています。マイDateクラスは'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 

ですが、私は、ページの後ろに私のコードのは、私は、このエラーを与えることに使用しようとしています中に問題がある - 「System.IConvertibleを入力するタイプ「Common.Date」のオブジェクトをキャストすることができません

私が使用しているコード Date purchaseDate = new Date(item.PurchaseDate); 日付submissionSate =新しい日付(item.SubmissionDate);ここの項目で

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

purchasedateとsubmision日付オブジェクトdatetime型のプロパティであり、誤りがあればライン である誰も私にどんなsuggesionを提供することはできますか?この問題の解決策は何でしょうか?

答えて

1

、あなたは

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

を持っており、あなたのDateオブジェクトを取るConvert.ToDateTimeのないオーバーロードはありませんので、あなたはIConvertibleを実装するためにobjectを必要Convert.ToDateTime(object)を呼んでいます。

IConvertibleを実装することも、_d1の値を@ChaosPandionの記述と比較することもできます。

+0

正しく理解できませんでしたか、もう少し説明してください。それはあなたのすてきなことです。 – Pankouri

+0

['Convert.ToDateTime'](http://msdn.microsoft.com/en-us/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#で学ぶために日付クラスを書いた後、それらを使用する方法を学びました。r --date1._d1 Pankouri

+0

@Pankouri - すべてのプライベートメンバーは、 'Date'の範囲内でアクセス可能です。 – ChaosPandion

関連する問題