我試圖找出在標題中,這發生在粗線這個片段中陳述如何解決錯誤: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);
}
}
}
該程序不知道你希望如何將字符串轉換爲整數列表。如果它是以某種分隔格式存在的,那麼在將其轉換爲整數列表之前,您需要單獨解析。 – BoltClock
你的問題不清楚。請解釋爲什麼您認爲您應該能夠隱式地將字符串值轉換爲List List的一個實例。請提供一個很好的[mcve],清楚地表明你正在嘗試做什麼,並提供關於該代碼預期做什麼的準確解釋。 –