Reputation: 77
I have one class which holds a question, and another that is a list of the question class. My goal is to be able to call the constructor of the question list class and pass in a file path to a CSV holding the information for 55 questions. The call should then take this information and generate the list of question objects relative to the data.
I have the information held in a string array however am unsure on how to assign this data to new question objects so I am then able to reference an individual question from the class instance of the question list.
Does anybody have any idea how to proceed? Here is my code so far:
class QuestionBank : List<Question>
{
public QuestionBank()
{
}
public static QuestionBank GetQuestions(string path)
{
using (FileStream fs = File.OpenRead(path))
{
byte[] bytes = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
StringBuilder sb = new StringBuilder();
while (fs.Read(bytes, 0, bytes.Length) > 0)
sb.Append(temp.GetString(bytes));
MessageBox.Show(sb.ToString());
string[] splitText = sb.ToString().Split(',');
}
QuestionBank bank = new QuestionBank()
{
};
return bank;
}
}
Upvotes: 0
Views: 27
Reputation: 440
I think you are looking for something like this:
public class QuestionBank
{
private string filePath = "test.csv";
public List<Question> Questions = new List<Question>();
public void GetAllQuestions()
{
foreach (var line in File.ReadAllLines(filePath))
{
Questions.Add(new Question
{
Value = line
});
}
}
}
public class Question
{
public string Value { get; set; }
}
Upvotes: 1