【发布时间】:2016-03-07 13:51:13
【问题描述】:
我正在尝试找出如何解决标题中所述的错误,该错误出现在此 sn-p 中的粗线:
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);
}
前面的代码行可以很好地加载名称,因为它不是我创建的类中的列表,但是“分数”在列表中。我需要做哪些修改?这些值应该在加载应用程序时显示在文本文件的文本框中。
这是“Scores”所在的班级(我已突出显示):
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);
}
}
}
【问题讨论】:
-
该程序不知道您希望如何将字符串转换为整数列表。如果它采用某种分隔格式,则需要在将其转换为整数列表之前单独对其进行解析。
-
您的问题不清楚。请解释为什么您认为您应该能够将
string值隐式转换为List<int>的实例。请提供一个良好的minimal reproducible example,清楚地表明您正在尝试做什么,并准确解释该代码的预期用途。
标签: c# arrays list syntax syntax-error