2013-04-11 48 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."); 
    } 
}