2015-12-03 74 views
-3

我試圖找出在標題中,這發生在粗線這個片段中陳述如何解決錯誤:C#爲什麼我不能隱式地將類型'字符串'轉換爲'System.Collections.Generic.List <int>'?

 while (textIn.Peek() != -1) 
     { 
      string row = textIn.ReadLine(); 
      string[] columns = row.Split('|'); 
      StudentClass studentList = new StudentClass(); 
      studentList.Name = columns[0]; 
      **studentList.Scores = columns[1];** 
      students.Add(studentList); 
     } 

的上一行代碼中加載的名字就好了,因爲它是不在我創建的類中有一個List,但是「分數」在列表中。我需要做些什麼修改?這些值應該在加載應用程序時從文本文件的文本框中顯示。

這是在「成績」是在(我強調它)類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace MyNameSpace 
{ 

//set the class to public 
public class StudentClass 
{ 

    public StudentClass() 
    { 
     this.Scores = new List<int>(); 


    } 

    public StudentClass (string Name, List<int> Scores) 
    { 
     this.Name = Name; 
     this.Scores = Scores; 
    } 



    public string Name 
    { get; 
     set; 
    } 

    //initializes the scores 
    **public List<int> Scores 
    { get; 
     set; 
    }** 


    public override string ToString() 
    { 
     string names = this.Name; 

     foreach (int myScore in Scores) 
     { names += "|" + myScore.ToString(); 

     } 
     return names; 
    } 


    public int GetScoreTotal() 
    { 
     int sum = 0; 

     foreach (int score in Scores) 
     { sum += score; 


     } 

     return sum; 

    } 

    public int GetScoreCount() 
    { return Scores.Count; 

    } 

    public void addScore(int Score) 
    { 
     Scores.Add(Score); 


    } 



} 
} 
+3

該程序不知道你希望如何將字符串轉換爲整數列表。如果它是以某種分隔格式存在的,那麼在將其轉換爲整數列表之前,您需要單獨解析。 – BoltClock

+1

你的問題不清楚。請解釋爲什麼您認爲您應該能夠隱式地將字符串值轉換爲List List的一個實例。請提供一個很好的[mcve],清楚地表明你正在嘗試做什麼,並提供關於該代碼預期做什麼的準確解釋。 –

回答

0

你不能只分配包含數字的序列List<int>類型的屬性的字符串。

您需要將字符串拆分爲單獨的數字,然後解析這些子字符串以獲取它們所代表的整數。

E.g.

var text = "1 2 3 4 5 6"; 
    var numerals = text.Split(' '); 
    var numbers = numerals.Select(x => int.Parse(x)).ToList(); 

即,在你的代碼替換:

studentList.Scores = columns[1]; 

有:(還是自己多行,更具可讀性/調試的當量)

studentList.Scores = columns[1].Split(' ').Select(int.Parse).ToList(); 

你需要修改傳遞到參數Split()根據分數如何格式化在您的列中。

如果您尚未擁有,您還需要在頂部添加using System.Linq;

+0

我想從一個文本文件中讀取一個列表框中的內容: John Doe | 97 | 71 | 83 | Daniel Lopez | 99 | 93 | 97 | Jane Doe | 100 | 100 | 99 | 但是我得到這個: 李四| 97 丹尼爾·洛佩茲| 99 李四| 100 如果我添加一個額外的列變量將只是分數的下一組的權利代替上述的分數它。像這樣: John Doe | 71 Daniel Lopez | 93 Jane Doe | 100 – ChocolateSnow

+0

@ChocolateSnow這聽起來像一個新問題,所以您應該將它作爲一個新問題發佈。另外,儘可能清楚地表達你的問題,因爲你的評論不是很清楚。 – Ergwun

0

就問題而言,編譯器如何知道如何將字符串轉換爲列表,以便列表可以有很多字符串表示形式。如果是這樣做,這將是一個非常緩慢的操作。

修復
要修復您的代碼,您可以用此代替您的循環。

while (textIn.Peek() != -1) 
{ 
    string row = textIn.ReadLine(); 
    StudentClass studentList = new StudentClass(); 
    int index = row.IndexOf("|"); 

    //checking that there were some scores 
    if (index < 0) { 
     studentList.Name = row; 
     continue; 
    } 

    studentList.Name = row.Substring(0, index); 
    studentList.Scores = row.Substring(index + 1).Split('|').Select(int.Parse).ToList(); 
    students.Add(studentList); 
} 

即使使用此修復程序,也存在一些問題。 如果您要添加由'|'分隔的另一個列表,使用這種方法解析會變得越來越困難。

相反,我建議你看看用類似Json.Net的更強大和通用的東西來序列化你的類。

希望這會有所幫助。

相關問題