我正在尝试创建列表列表,其中大列表示纸张包含小列表代表问题的集合,问题列表包含问题字符串及其ID。 在这里我的代码:
public class Genes
{
public string question { get; set; }
public int CLO { get; set; }
}
List<Genes> questiongene = new List<Genes>();
List<List<questiongene>> paper = new List<List<questiongene>>();
现在我没有错误地制作问题列表但是当我尝试创建更大的列表时,visual studio无法将变量问题类型识别为类型,哪里出错了?
答案 0 :(得分:2)
我会这样做,而不是建议的基本List<List<Genes>>
,因为这清楚了每个项目是什么......
//This is your class for a single question. A list of these will
//make up a paper.
public class Genes
{
public string Question{ get; set; }
public int CLO { get; set; }
}
//This is a Paper, which contains a list of Questions.
public class Paper
{
public List<Genes> Questions { get; set; }
}
..
..
//To use: First create a list of papers -
//this is a stack of papers.
List<Paper> stackOfPapers = new List<Paper>();
//now create a list of questions which we can add to a paper
List<Genes> questions = new List<Genes>();
//Now we need to create a question to add to the list of questions.
Genes newQuestion = new Genes();
newQuestion.Question = "How many roads must a man walk down?";
newQuestion.CLO = 42;
//now add the question to the list.
questions.Add(newQuestion);
//now we need to create a paper. This will represent one
//paper in our stack of papers.
Paper newPaper = new Paper();
//add our list of Questions to the paper.
newPaper.Questions = questions;
//and finally, add the paper to the stack of papers.
stackOfPapers.Add(newPaper);
//or alternatively, you can use the object initializer syntax to
//do this all in one. Note I've also added more than one paper to
//the list of papers, and each paper has two questions contained in it:
var newStackOfPapers =
new List<Paper>
{
new Paper
{
Questions = new List<Genes> {
new Genes
{
Question = "How many roads must a man walk down?",
CLO = 42
},
new Genes
{
Question = "Another Question?",
CLO = 111
}
}
},
//add Another paper...
new Paper
{
Questions = new List<Genes> {
new Genes
{
Question = "This is the first question on the second paper?",
CLO = 22
},
new Genes
{
Question = "Another Question?",
CLO = 33
}
}
},
};
答案 1 :(得分:0)
您的代码中没有类型的questiongene,只有Genes的类型。 创建列表列表:
List<List<Genes>> paper = new List<List<Genes>>();
或创建一个具有List questiongene的新类型纸张:
public class Paper
{
public List<Genes> questionGenes { get; set; }
// default constructor
public Paper()
{
questionGenes = new List<Genes>();
}
// constructor that creates a paper from a List<Genes>
public Paper(List<Genes> questionGene)
{
questionGenes = questionGene;
}
}
然后创建一个Paper列表:
List<Paper> papers = new List<Paper>();
papers.Add(new Paper(questiongene));