【问题标题】:Why can't it convert to a double or int or string from Car_Management.Car为什么它不能从 Car_Management.Car 转换为 double 或 int 或字符串
【发布时间】:2023-03-14 02:47:01
【问题描述】:

我遇到了一个问题,我收到一条错误消息,指出我无法从“double”转换为“Car_Management.Car”。我在这里想念什么?这是我到目前为止所做的代码,我必须将汽车特性添加到列表中。另外,如何将列表输出到文本文件?

namespace Car_Management
{
    class CarDM
    {
        public static void save(List<Car> fullList)
        {
            Car myCar = new Car();
            List<Car> list = new List<Car>() {myCar.Model, myCar.Mileage, myCar.Colour, myCar.Year};

            StreamWriter outputFile;
            outputFile = File.CreateText("List.txt");
        }
        public static void load()
        {
            StreamReader inputFile;
            inputFile = File.OpenText("List.txt");
        }
    }
}

这是我的另一门课

namespace Car_Management
{
    class Car
    {
        private int year;
        private double mileage;
        private string colour;
        private string model;
        private string info;

        //constructor
        public Car()
        {
            year = 0;
            mileage = 0;
            colour = "";
            model = "";
        }

        //name properties
        public string Model
        {
            get { return model; }
            set { model = value; }
        }

        public string Colour
        {
            get { return colour; }
            set { colour = value; }
        }

        public double Mileage
        {
            get { return mileage; }
            set { mileage = value; }
        }

        public int Year
        {
            get { return year; }
            set { year = value; }
        }

        //returns string consisting of model, year, mileage, colour
        public string GetInfo(string sep)
        {
            sep = ": ";
            return model + sep + colour + sep + mileage.ToString("c") + sep + year.ToString();
        }
    }
}

【问题讨论】:

  • List&lt;Car&gt; list = new List&lt;Car&gt;() {myCar.Model, myCar.Mileage, myCar.Colour, myCar.Year}; - 你到底打算做什么?您告诉它您正在尝试创建 Car 的列表,但提供的是 Car 的组件,而不是实际的 Car 对象。
  • 您应该为您创建的对象赋值。例如 List list = new List() {myCar.Model = "Malibu", myCar.Mileage = 150, myCar.Colour = color.Red, myCar.Year = 2015};
  • 开始阅读 MSDN 文档并了解 .ToStrng() 重载 msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

标签: c# variable-assignment


【解决方案1】:

免责声明:我什至没有查看您使用流阅读器的加载/写入方法,因为您在那里没有实现。如果您需要序列化方面的帮助,请提出另一个问题。

您的问题是初始化程序的语法错误。以下是可以使对象实例化的有效方法。详情请见Object and Collection Initializers

Car myCar = new Car(); // no initializers
Car myCar = new Car(){Model = "Bently", Mileage = 1555, Colour = "Red", Year = 2015}; // with initializer

List&lt;Car&gt; 的示例

List<Car> list = new List<Car>(); // no initializers
List<Car> list = new List<Car>() {myCar} ; // with a car instance
List<Car> list = new List<Car>() {myCar1, myCar2, myCar3} ; // with car instances assuming these instances all exist

你也可以把它们组合起来,虽然看起来不是很干净

List<Car> list = new List<Car>() {new Car(){Model = "Bently", Mileage = 1555, Colour = "Red", Year = 2015}} ; // with a car instance

附带说明,您可以将您的属性转换为 AutoProperties,这也有助于清理您的代码。

namespace Car_Management
{
    class Car
    {
        public Car()
        {
            Year = 0;
            Mileage = 0;
            Colour = "";
            Model = "";
        }

        //name properties
        public string Model {get;set;}
        public string Colour {get;set;}
        public double Mileage {get;set;}
        public int Year {get;set;}

        //returns string consisting of model, year, mileage, colour
        public string GetInfo(string sep)
        {
            sep = ": ";
            return model + sep + colour + sep + mileage.ToString("c") + sep + year.ToString();
        }
    }
}

【讨论】:

    【解决方案2】:

    您对列表的实例化是错误的。

    public static void save(List<Car> fullList)
    {
        Car myCar = new Car();
        List<Car> list = new List<Car>() {myCar.Model, myCar.Mileage, myCar.Colour, myCar.Year};
    
        StreamWriter outputFile;
        outputFile = File.CreateText("List.txt");
    }
    

    你在这里要做的是实例化一个字符串、双精度、字符串等列表。

    你应该做的是:像这样预先创建汽车:

    Car myCar = new Car();
    myCar.Model = "Some Model";
    myCar.Mileage = 0;
    ...
    
    List<Car> list = new List<Car>();
    list.add(myCar);
    

    或者如你所愿:

    List<Car> list = new List<Car>(){myCar};
    

    或者如果您想一次性完成所有操作(注意:乱七八糟的代码):

    List<Car> list = new List<Car(){new Car(){Model = "Some model", Mileage = 0, ... }}
    

    我个人更喜欢第一种方法,因为它更容易阅读,并且之后更容易深入代码。

    如果您有几辆汽车,您可以执行以下操作:List&lt;Car&gt; list = new List&lt;Car&gt;(){car1, car2, car3}; AFTER 创建汽车对象后,如上所示。希望对您有所帮助。

    您可以在这里阅读更多内容:http://www.dotnetperls.com/list

    【讨论】:

      【解决方案3】:

      此语法不正确:

      Car myCar = new Car(); List<Car> list = new List<Car>() {myCar.Model, myCar.Mileage, myCar.Colour, myCar.Year}; 而且我不确定您要做什么。如果你想创建一个包含一辆车的列表,你应该写:

      Car myCar = new Car(){ Model = "model1", Mileage = 23000, Colour = "green", Year = 2009 }; List<Car> list = new List<Car>(); list.Add(myCar);

      这将使代码编译。但是还有其他问题,例如未使用的参数 fullList,并且您正在创建一个文件“List.txt”并且从未写入它。

      【讨论】:

        猜你喜欢
        • 2018-10-26
        • 2014-03-24
        • 2014-11-18
        • 1970-01-01
        • 1970-01-01
        • 2013-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多