foreach声明无法操作(GetEnumerator)请帮帮我

时间:2015-03-20 00:40:49

标签: c# winforms for-loop

无法分配给'cell',因为它是'foreach迭代变量'

foreach (var cell in cells)
  {
      var temp = cell * 0.3048;
        // convert  to meter
      // cell = cell * 0.3048;
        if (temp < Fluid)
        {
            double layer = Fluid - temp;
            if (layer > based)
            {
                layer = based;
            }
            double value = gridSize * gridSize * layer;
            totalValue += value;
        }
    }

我又有一个与此部分相关的错误: foreach语句不能对'double'类型的变量进行操作,因为'double'不包含'GetEnumerator'的公共定义

`private double[] volumcalculate(double cells, double gridSize, double based, double Fluid) 
     {

   // 1. cubic meter, 2. cubic feet, 3. barrels

    double[] values = new double[3]; 
    if (cells==null){
    return values; 
        double totalValue = 0;
    // convert  to meter
    gridSize = gridSize * 0.3048;

  foreach (var cell in cells)
  {
      var temp = cell * 0.3048;
        // convert  to meter
      // cell = cell * 0.3048;
        if (temp < Fluid)
        {
            double layer = Fluid - temp;
            if (layer > based)
            {///

我从以下部分获取细胞和细胞:

 public List<double> readFileToList(string Path)
    {

        var cells = new List<double>();
        string path = label3.Text;

        if (File.Exists(path))
        {
            double temp = 0;
            cells.AddRange(File.ReadAllLines(path)
                .Where(line => double.TryParse(line, out temp))
                .Select(l => temp)
                .ToList());
            int totalCount = cells.Count();
            cellsNo.Text = totalCount.ToString();

        }

       return cells;

    }

  private void button1_Click_1(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            label3.Text = openFileDialog1.FileName;
            // textBox1.Text = File.ReadAllText(label3.Text);

            string path = label3.Text;
          readFileToList(path);

                                     }enter code here

1 个答案:

答案 0 :(得分:1)

根据此处的评论和删除的评论,在您的定义中,单元格似乎不是List而只是一个double。

高级示例看起来像这样

var cells = new List<double>;
foreach(var cell in cells)
{
    var toWorkWith = cell * 0.3048;
    //Rest of code here
}

假设您实际上将使用值填充单元格,而不是仅将其设置为新列表。如果这不起作用,请检查单元格对象的类型