2016-04-27 43 views
-4

我遇到了一個問題,我收到一個錯誤,說我無法從'double'轉換爲'Car_Management.Car'。我在這裏錯過了什麼?這是我迄今爲止所做的代碼,我必須將汽車特性添加到列表中。另外,我怎樣才能將列表輸出到文本文件?爲什麼不能從Car_Management.Car轉換爲double或int或字符串

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(); 
     } 
    } 
} 
+4

'List list = new List (){myCar.Model,myCar.Mileage,myCar.Colour,myCar.Year};' - 你打算做什麼?你告訴它你試圖創建一個'Car'的列表,但是提供一個'Car'的組件,而不是一個實際的Car'對象。 –

+0

您應該將值分配給您創建的對象。例如列表 list = new List (){myCar.Model =「Malibu」,myCar.Mileage = 150,myCar.Colour = color.Red,myCar.Year = 2015}; – Auguste

+0

開始閱讀MSDN文檔,並瞭解'.ToStrng()'重載https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx – MethodMan

回答

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(); 
     } 
    } 
} 
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"); 
} 

什麼you're試圖在這裏做的就是實例化字符串,雙,字符串等的列表

你應該做的是兩種:創建前手像這樣的車:

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

,或者如果你想要做的這一切在一個去(注:混亂和unreadabl E碼):

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

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」文件並且從不寫入。

相關問題