2013-04-11 55 views
-8

我有一個簡單的自定義列表類,我想實現IComparable它,但它不工作是誠實的。我試過MSDN和其他博客,但仍然一樣。嵌入式語句錯誤

public class sortDateTime : IComparable 
{ 
    protected DateTime m_startDate, m_endDate; 

    public DateTime startDate 
    { 
     get { return m_startDate; } 
     set { m_startDate = startDate; } 
    } 

    public DateTime endDate 
    { 
     get { return m_endDate; } 
     set { m_endDate = endDate; } 
    } 

    public int CompareTo(object obj) 
    { 
     if(obj is sortDateTime) 
      sortDateTime sDT = (sortDateTime) obj; //here ERROR 

     return m_stDate.CompareTo(sDT.m_stDate); 
    } 
} 

其次this example,但得到的錯誤:

Embedded statement cannot be a declaration or labeled statement

+0

我只想最新的(不是更早)開始日期 – user2262511 2013-04-11 10:41:01

+0

它是怎麼在MSDN例如 – user2262511 2013-04-11 10:42:58

+4

的http://計算器。com/questions/2496589/variable-declarations-following-if-statements – 2013-04-11 10:43:25

回答

4

請看一看的一段代碼,從而導致錯誤:

if(obj is sortDateTime) 
    sortDateTime sDT = (sortDateTime) obj; //here ERROR 

return m_stDate.CompareTo(sDT.m_stDate); 

你所說的是這樣的:

if the object is of type 'sortDateTime' 
    Allocate memory for variable 'sDT' 
    Cast 'obj' to type 'sortDateTime' 
    Store the result in variable 'sDT' 

然後你離開的範圍 - 變量不再需要(它被分配在'堆棧'上並被釋放)。這根本不符合邏輯。這是一個操作,即執行無效。你想要做的是以下幾點:

// Variable for remembering the "cast result" after the cast 
sortDateTime sDT = null; 

if (obj is sortDateTime) 
    sDT = (sortDateTime)obj; // Cast the object. 
else 
    return 0;     // "obj" is not an "sortDateTime", so we can't compare. 

// Return the comparison result, if we can compare. 
return m_stDate.CompareTo(sDT.m_stDate); 

編譯器注意到你不能做這樣的操作並引發錯誤。然而,這將彙編:

if (obj is sortDateTime) 
{ 
    sortDateTime sDT = (sortDateTime)obj; 
} 

,但它不會在

m_stDate.CompareTo(sDT.m_stDate); // sDT is not a variable in scope. 

意義要麼,並導致編譯器錯誤這是我將如何實現的方法:

sortDateTime sDT = obj as sortDateTime; // 'as' leads to an casted object, or null if it could not cast 

if (sDT == null) 
    throw new NotSupportedException("The object is not an sortDateTime"); 
else 
    return m_stDate.CompareTo(sDT.m_stDate); 

乾杯!

0

就這樣做:

if(obj is sortDateTime) { 
    sortDateTime sDT = (sortDateTime) obj; //here ERROR 
} 

,它會消失。

有關一個更具體的解釋,爲什麼編譯器這樣的行爲,請旁觀: Why this compile error

C#標準的命名習慣:不命名你的類型開始與非大寫字母,所以更改sortDateTime - >SortDateTime

+0

仍然是相同的錯誤 – user2262511 2013-04-11 10:45:06

+0

不應該包含if語句中的return語句嗎? – 2013-04-11 10:45:17

+0

@caerolus:不這麼認爲。不知道這個應用程序的工作流程是什麼。 – Tigran 2013-04-11 10:45:50

2

不檢查你的邏輯,我會解決語法錯誤。

此:

public int CompareTo(object obj) 
{ 
    if(obj is sortDateTime) 
     sortDateTime sDT = (sortDateTime) obj; //here ERROR 

    return m_stDate.CompareTo(sDT.m_stDate); 
} 

應該是:

public int CompareTo(object obj) 
{ 
    if (obj is sortDateTime) 
    { 
     sortDateTime sDT = (sortDateTime) obj; 
     return m_startDate.CompareTo(sDT.m_startDate); 
    } 
    else 
    { 
     throw new ArgumentException("object is not a sortDateTime "); 
    } 
} 

更仔細你的鏈接頁面。你沒有正確地遵守。

+0

歡呼聲,想通了 – user2262511 2013-04-11 10:50:53

0

試試這個:

public int CompareTo(object obj) 
{ 
    sortDateTime sDT = null; 
    if(obj is sortDateTime) 
     sDT = (sortDateTime) obj; //here ERROR 

    if(sDT != null) 
    { 
     return m_stDate.CompareTo(sDT.m_stDate); 
    } 
    else 
    { 
     throw new ArgumentException("object is not a sortDateTime type."); 
    } 
}