Reputation: 1303
How can I get var finalQuestions
from class Question to my GroupExmStart Form in this string QuestionSet = "";
? How I can get it in Form and what conversions do I need to do to store it as string? I want to store it as string because this QuestionSet I have to pass here: int z = Quiz(QuestionSet);
,how could I do this? Is their a better way with which I can achieve this?
GroupExmStart Form
public partial class GroupExmStart : Form
{
string QuestionSet = "";
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('|');
qsn.Foo(TopiID, conf);
int z = Quiz(QuestionSet);
}
int Quiz(string data)
{
string[] words = data.Split('$');
randomQsn = new string[totQsn + 1];
QAndA = new string[words.Length + 1];
for (int i = 0; i < words.Length; i++)
{
QAndA[i] = words[i];
}
return 0;
}
private void Question(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 from Questions where TopicID IN(" +
topicId + ") and Marks=" + marks.ToString();
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 void 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();
//this finalQuestions I want to access in my Form
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);
}
}
}
Upvotes: 0
Views: 575
Reputation: 6840
Have method Foo return the final questions variable...
public List<Question> Foo(string TopicId, string[] conf)
{
// do all the same stuff...
return finalQuestions;
}
Then, in your GroupExmStart method do the following...
Question qsn = new Question();
string[] conf = db.GetConfiguration(Convert.ToInt16(DurationID)).Split('|');
QuestionSet = string.Join(",", qsn.Foo(TopiID, conf));
int z = Quiz(QuestionSet);
Edit per comment #1
A better way, IMO, would be to not join them to begin with; and just pass the list of questions into the Quiz method directly. So you would have to alter the Quiz method to take a list of Questions like:
int Quiz(List<Question> questions)
And then remove the string.Join from above.
Question qsn = new Question();
string[] conf = db.GetConfiguration(Convert.ToInt16(DurationID)).Split('|');
var questions = qsn.Foo(TopiID, conf);
int z = Quiz(questions);
Edit per comment #3
private void Question(Question q)
{
label5.Text = string.Format("{0} {1}", q.Id, q.Text);
radioButton12.Text = q.Option1;
radioButton11.Text = q.Option2;
radioButton10.Text = q.Option3;
radioButton9.Text = q.Option4;
}
Upvotes: 2