将Class方法的返回值显示为Form
本文关键字:显示 Form 返回值 Class 方法 | 更新日期: 2023-09-27 18:27:00
在Form GroupExmStart
中,我从Question类调用Foo method
,将所有Questions存储在var questions
中,并将其传递给int Quiz method
,我如何将此question
存储在string[]
中,以便使用DisplayQuestions()
显示我的问题,我尝试使用string[] ab=questions.ToArray();
将其转换为字符串,但不起作用,是转换不可能还是哪里出了问题?
Form 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;
}
}
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;
}
如果你需要显示问题,你最好覆盖问题类的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;
}
您的问题是
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();
}
试试这个,
string _que = string.Empty;
foreach (var item in questions)
{
_que =Convert.toString(item.Text);
}
return 0;
由于Question
是类,Text
是其属性之一,因此u可以通过其对象访问它。