c# - Display returned value from Class method to Form -
in form groupexmstart
calling foo method
question class , storing questions in var questions
, passing int quiz method
,how can store question
in string[]
can display questions using displayquestions()
,i tried convert string using string[] ab=questions.toarray();
not working , conversion not possible or going wrong ?
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 not getting how do getting `item 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; } } }
class question
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 questions topicid in(" + topicid + ") , 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 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>(); (int = 0; < mark1qsn; i++) { var setindex = _random.next(mark1questionset.count); finalquestions.add(mark1questionset[setindex]); mark1questionset.removeat(setindex); } (int = 0; < 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) { // question // question.id, question.text etc.. can access here } return 0; }
if need display question better override tostring method of question class below
public class question { public override string tostring() { // can change wish return string.formt("id:{0}, text :{1}", id, text); }
then can try below
int quiz(list<question> questions) { foreach (question question in questions) { messagebox.show(question.tostring()); } return 0; }
or can use existing method below few modifications
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; }
Comments
Post a Comment