gpt4 book ai didi

c# - 显示从 Class 方法返回的值到 Form

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:52 25 4
gpt4 key购买 nike

Form GroupExmStart 中,我从 Question 类调用 Foo 方法 并将所有问题存储在 var questions 中并将其传递给 int测验方法,如何将此问题存储在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;
}
}

最佳答案

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;
}

关于c# - 显示从 Class 方法返回的值到 Form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18118325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com