2

Form GroupExmStartFoo method从 Question 类调用并将所有问题存储在var questions其中并将其传递给int Quiz method,我如何将其存储questionstring[]其中以便我可以使用显示我的问题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;
        }        
    } 
4

3 回答 3

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;
}
于 2013-08-08T05:16:47.940 回答
0

你的问题是

var str = questions.ToArray();

返回一个问题类型的数组

Question[]

而不是字符串类型的数组。

您必须为 Question 创建一个重载以输出您需要的字符串。

就像是

public class Question
{
    public override string ToString()
    {
        return Text;
    }
    ....

这应该允许你做类似的事情

foreach (var item in str)
{
    string itemText = item.ToString();
}
于 2013-08-08T05:18:14.540 回答
0

尝试这个,

    string _que = string.Empty;
    foreach (var item in questions)
    {
        _que =Convert.toString(item.Text);      
    }                
    return 0;

因为Question是类并且Text是它的属性之一,所以你可以通过它的对象访问它。

于 2013-08-08T05:23:23.393 回答