【问题标题】:catch response from call [closed]从呼叫中获取响应[关闭]
【发布时间】:2013-05-22 07:49:14
【问题描述】:

我的 foreach 语句中出现此错误。

foreach statement cannot operate on variables of type because does not contain a public definition for 'GetEnumerator'.

这是什么意思,我该如何解决?

更新

这里有一些代码:

    ServiceReference.TimereportResponseDto[] response;
    foreach (ServiceReference.TimereportResponseDto r in response)
    arbu.Text += r.Message + " " + r.Id + Environment.NewLine; 

在第一个精简版中添加了 [ ],现在我又遇到了另一个错误:

Use of unassigned local variable 'response'

更新 2

我有这个文档示例,它看起来像这样:

Gateway.TimereportResponseDto[] response = theGate.CreateTimereport(timeReport); 
foreach (Gateway.TimereportResponseDto r in response) 
textBoxLog.Text += r.Message + " " + r.Id + Environment.NewLine; 

【问题讨论】:

  • 能否提供代码?
  • 你正在“foreaching”的变量不是一个集合。你能发布更多代码吗?
  • 确保您尝试迭代的变量有一个名为“GetEnumerator”的公共方法。
  • 如果您正在寻找答案,请提出问题并提供详细信息
  • 第二个错误是因为你没有用任何东西初始化response。错误代码非常具有描述性..

标签: c# windows-phone-7 foreach compiler-errors


【解决方案1】:

这意味着你不能对你想要的对象执行foreach,因为它没有公开GenEnumerator-方法,由IEnumerable (IEnumerable<T>) 实现。似乎它不是 Collection-object。

MSDN:

IEnumerable 接口

Exposes the enumerator, which supports a simple iteration over a non-generic collection.

【讨论】:

    【解决方案2】:

    您没有迭代实现(IEnumerableIEnumerable<T>)的对象,(例如数组或列表)

    foreach - MSDN

    foreach 语句为每个语句重复一组嵌入语句 数组或对象集合中的元素实现 System.Collections.IEnumerableSystem.Collections.Generic.IEnumerable<T>界面.

    【讨论】:

      【解决方案3】:

      来自MSDN;

      foreach 语句为每个语句重复一组嵌入语句 元素在数组中实现 System.Collections.IEnumerable 或 System.Collections.Generic.IEnumerable 接口.

      看起来你应该在你的类中引入 IEnumerable 接口。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-30
        • 2017-01-28
        • 2021-01-15
        • 1970-01-01
        • 1970-01-01
        • 2019-12-30
        • 1970-01-01
        • 2020-06-30
        相关资源
        最近更新 更多