我需要帮助来解决错误

时间:2014-02-24 08:22:54

标签: c#

在这里,我正在尝试编写一个程序,该程序从文本文件接收输入值并将值分配给适当的变量。但我面临的错误如下:

  

System.Collections.Generic.List'没有   包含'length'的定义,没有扩展方法'length'   接受第一个类型的参数   'System.Collections.Generic.List'可以   找到(你是否错过了使用指令或汇编参考?

即使对于高度,深度音量也会出现相同的错误。请妥善解决此错误。

using System;
using System.IO;
using System.Collections.Generic;


namespace ReadInputDetails
{
    class Boxdetails
    {
        private string boxno;
        private double length;
        private double height;
        private double depth;
        private double volume;

        // Declare a number of box of type string:
        public string box_no
        {
            get 
            {
               return boxno; 
            }
            set 
            {
               boxno = value; 
            }
        }

        // Declare  properties of box of type double:
        public double box_length
        {
            get 
            { 
               return length; 
            }
            set 
            { 
               length = value; 
            }
        }
        public double box_height
        {
            get
            {
                return height;
            }
            set
            {
                height = value;
            }
        }
        public double box_depth
        {
            get
            {
                return depth;
            }
            set
            {
                depth = value;
            }
        }
        public double box_volume
        {
            get
            {
                return volume;
            }
            set
            {
                volume = value;
            }
        }

            public static void getdetailsofbox(string[] args)
            {
                try
                {
                    List<Boxdetails> box = new List<Boxdetails>();

                    using (StreamReader sr = new StreamReader("c:/containervalues.txt"))
                    {
                        string line;

                        while ((line = sr.ReadLine()) != null)
                        {
                            // create new instance of container for each line in file
                            Boxdetails b = new Boxdetails();
                            string[] Parts = line.Split(' ');
                            // set non-static properties of container
                            box.boxno = Parts[0];
                            box.length = Convert.ToDouble(Parts[1]);
                            box.height = Convert.ToDouble(Parts[2]);
                            box.depth = Convert.ToDouble(Parts[3]);
                            box.volume = Convert.ToDouble(Parts[4]);
                            // add container to list of containers
                            b.Add(box);
                        }
                    }

                }
                catch (Exception ex)
                {
                    // what failed?
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:4)

您要为列表分配值,而不是box对象。变化:

Boxdetails b = new Boxdetails();
string[] Parts = line.Split(' ');
// set non-static properties of container
box.boxno = Parts[0];
box.length = Convert.ToDouble(Parts[1]);
box.height = Convert.ToDouble(Parts[2]);
box.depth = Convert.ToDouble(Parts[3]);
box.volume = Convert.ToDouble(Parts[4]);
// add container to list of containers
b.Add(box);

要:

Boxdetails b = new Boxdetails();
string[] Parts = line.Split(' ');
// set non-static properties of container
b.box_no = Parts[0];
b.box_length = Convert.ToDouble(Parts[1]);
b.box_height = Convert.ToDouble(Parts[2]);
b.box_depth = Convert.ToDouble(Parts[3]);
b.box_volume = Convert.ToDouble(Parts[4]);
// add container to list of containers
box.Add(b);

此外,请考虑将框列表命名为listOfBoxes或仅boxes,并将BoxDetails类重命名为Box,因为它代表一个框。然后你可以写boxes.add(box);哪个更有意义,你不同意吗?

旁注:你可能想要遵守一个名为naming guidelines(以及coding conventions)的东西。