为什么不能从Car_Management.Car转换为double或int或字符串

时间:2016-04-27 20:20:54

标签: c# variable-assignment

我正在遇到一个问题,我收到一条错误,说我无法将'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();
        }
    }
}

3 个答案:

答案 0 :(得分:0)

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

您的问题是您的初始化程序的语法错误。以下是可以实例化对象的有效方法。有关详细信息,请参阅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<Car>

的示例
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();
        }
    }
}

答案 1 :(得分:0)

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

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

或者如果你想一气呵成(NB:凌乱且不可读的代码):

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

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

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

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

答案 2 :(得分:0)

此语法不正确:

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”而从不写入它。