【问题标题】:Embedded statement error [duplicate]嵌入式语句错误[重复]
【发布时间】:2013-04-11 10:40:25
【问题描述】:

我有一个简单的自定义列表类,我正在尝试对其实现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,但收到错误:

嵌入式语句不能是声明或带标签的语句

【问题讨论】:

  • 我只想要最新(而不是更早)的开始日期
  • 它在 MSDN 示例中的样子
  • 不要嵌入语句,即在 if 语句中添加花括号。
  • 您只需完全按照您在问题中包含的 MSDN 示例进行操作。在if 之后打开{;在return 之后关闭};最后抛出ArgumentException,以捕获要比较的对象类型错误的情况;享受...如果您不掌握复制和粘贴的艺术,编程会变得非常困难!

标签: c# c#-2.0 icomparable


【解决方案1】:

请看一段代码,导致报错:

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);

干杯!

【讨论】:

    【解决方案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 ");   
        }
    }
    

    仔细查看您链接的页面。你没有正确地遵循它。

    【讨论】:

      【解决方案3】:

      就这样吧:

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

      它会消失的。

      有关为什么编译器会以这种方式运行的更具体解释,请查看: Why this compile error

      遵循C# 标准命名约定:不要以非大写字母开头的类型命名,因此更改sortDateTime -> SortDateTime

      【讨论】:

      • 你不应该在 if 中包含 return 语句吗?
      • @caerolus:不这么认为。不知道这个应用的工作流程是什么。
      • @Tigran 你的代码和他的基本一样。查看return语句,你会发现他需要保留被投射的对象
      • @voo:OP报告的错误是当你在单行语句中声明变量而不使用大括号时出现的错误。要了解原因,请按照提供的墨水并阅读 Skeet 的回答。
      • @downvoter:任何理智原因?
      【解决方案4】:

      试试这个:

      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.");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-03-14
        • 2013-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-22
        • 2023-04-07
        相关资源
        最近更新 更多