在Form GroupExmStart
我从问题类调用Foo method
并将所有问题存储在var questions
并将其传递给int Quiz method
,如何存储此question
在string[]
,以便我可以使用DisplayQuestions()
显示我的问题,我尝试使用string[] ab=questions.ToArray();
将其转换为字符串,但它不起作用,转换是不可能的还是我要去的地方错了?
表格GroupExmStart
public partial class GroupExmStart : Form
{
public GroupExmStart(string GroupName, string DurationID)
{
InitializeComponent();
this.GrpID=GroupName;
TopiID=db.GetTopicIDForGroup(GrpID);
Question qsn = new Question();
string[] conf = db.GetConfiguration(Convert.ToInt16(DurationID)).Split('|');
var questions = qsn.Foo(TopiID, conf);
int z = Quiz(questions);
int count = 0;
timer1.Interval = Convert.ToInt16(conf[1]) * 1000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
int Quiz(List<Question> questions)
{
var str = questions.ToArray();
foreach (var item in str)//I am not getting how do i do it Getting `item as namespaceName.Question`
{
}
return 0;
}
private void DisplayQuestion(string id, string Q, string OP1, string OP2, string OP3, string OP4)
{
label5.Text = Q;
radioButton12.Text = OP4;
radioButton11.Text = OP4;
radioButton10.Text = OP4;
radioButton9.Text = OP4;
}
}
}
课程问题
public class Question
{
public string Id { get; set; }
public string Text { get; set; }
public string Option1 { get; set; }
public string Option2 { get; set; }
public string Option3 { get; set; }
public string Option4 { get; set; }
public string AnswerOption { get; set; }
public int Marks { get; set; }
Random _random = new Random();
public IEnumerable<Question> GetQuestions(string topicId, int marks)
{
string sql = "select QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks from Questions where TopicID IN(" +
topicId + ") and Marks=" + marks;
var cmd = new OleDbCommand(sql,acccon);
var rs = cmd.ExecuteReader();
if (rs != null)
{
while (rs.Read())
{
yield return
new Question
{
Id = rs[0].ToString() + "~",
Text = rs[1].ToString() + "~",
Option1 = rs[2].ToString() + "~",
Option2 = rs[3].ToString() + "~",
Option3 = rs[4].ToString() + "~",
Option4 = rs[5].ToString() + "~",
AnswerOption = rs[6].ToString() + "~",
Marks = marks
};
}
}
}
public List<Question> Foo(string TopicId, string[] conf)
{
var totQsn = Convert.ToInt16(conf[0]);
var mark1qsn = Convert.ToInt16(conf[3]); //this variable contains number of question to be display of mark 1
var mark2qsn = Convert.ToInt16(conf[4]);
var mark3qsn = Convert.ToInt16(conf[5]);
var mark4qsn = Convert.ToInt16(conf[6]);
var mark1questionSet = GetQuestions(TopicId, 1).ToList();
var mark2questionSet = GetQuestions(TopicId, 2).ToList();
var finalQuestions = new List<Question>();
for (int i = 0; i < mark1qsn; i++)
{
var setIndex = _random.Next(mark1questionSet.Count);
finalQuestions.Add(mark1questionSet[setIndex]);
mark1questionSet.RemoveAt(setIndex);
}
for (int i = 0; i < mark2qsn; i++)
{
var setIndex = _random.Next(mark2questionSet.Count);
finalQuestions.Add(mark2questionSet[setIndex]);
mark2questionSet.RemoveAt(setIndex);
}
return finalQuestions;
}
}
答案 0 :(得分:2)
int Quiz(List<Question> questions)
{
foreach (Question question in questions)
{
// do something with question
// question.Id, question.Text etc.. can access from here
}
return 0;
}
如果您需要显示问题,最好覆盖Question类的ToString方法,如下所示
public class Question
{
public override string ToString()
{
// you can change this as you wish
return string.Formt("Id:{0}, Text :{1}", Id, Text);
}
然后你可以尝试下面的
int Quiz(List<Question> questions)
{
foreach (Question question in questions)
{
MessageBox.Show(question.ToString());
}
return 0;
}
或者您可以使用以下现有方法进行少量修改
int Quiz(List<Question> questions)
{
foreach (Question question in questions)
{
DisplayQuestion(question);
}
return 0;
}
private void DisplayQuestion(Question question)
{
label5.Text = question.Text;
radioButton12.Text = question.Option1;
radioButton11.Text = question.Option2;
radioButton10.Text = question.Option3;
radioButton9.Text = question.Option4;
}
答案 1 :(得分:0)
你的问题是那个
var str = questions.ToArray();
返回Question
类型的数组Question[]
而不是字符串类型的数组。
你必须为Question创建一个重载输出到你需要的字符串。
像
这样的东西public class Question
{
public override string ToString()
{
return Text;
}
....
这应该允许你做类似
的事情foreach (var item in str)
{
string itemText = item.ToString();
}
答案 2 :(得分:0)
试试这个,
string _que = string.Empty;
foreach (var item in questions)
{
_que =Convert.toString(item.Text);
}
return 0;
由于Question
是类,Text
是其属性之一,因此您可以通过其对象访问它。