简体   繁体   中英

How to use the data from a list of a class?

Currently I'm working on a quiz game using windows form application. So what I'm confused on is how to assign the data I read from Trivia.cs (from the ListOfData method) to the variables (questionText) and (answers) in TriviaForm, so that it can be passed to the Question.cs to process the data and to figure out which one is the right answer.

I'm just a beginner and this is my high school introductory class so I'd really appreciate the help.

So I made a Trivia class (Trivia.cs) to read the data.

namespace D4
{
    class Trivia
    {
        // Fields
        private const string FileName = "WumpusTrivia.txt";
        private string questionText;
        private string[] answers = new string[4];

        public Trivia()
        {

        }

        /// This method will return the list of data.
        public List<Question> ListOfData()
        {
            List<Question> ls = new List<Question>();
            using (StreamReader sr = new StreamReader(FileName))
            {
                while (!sr.EndOfStream)
                {
                    this.questionText = sr.ReadLine();
                    for (int i = 0; i < 4; i++)
                        this.answers[i] = sr.ReadLine();
                    sr.ReadLine();
                    ls.Add(new Question(this.questionText, this.answers)); 
                }
            }
            return ls;
        }
    }
}

A Question class (Question.cs) to process the data and to return the values to put into text boxes.

namespace D4
{
    class Question
    {
        private string question;
        private string[] answers = new string[4];
        private int correctAnswer;
        private string[] freeTrivia;
        private Trivia trivia;

        public Question(string question, string[] answers)
        {
            this.trivia = new Trivia();

            this.question = question;
            this.answers = answers;
            for (int i = 0; i < 4; i++)
            {
                if (answers[i].ToString().EndsWith("+"))
                {
                    this.correctAnswer = i;
                    answers[i].ToString().Replace('+', ' ');
                }
            }
        }

        public string QuestionText { get { return this.question; } }
        public string[] Answers { get { return this.answers; } }
        public int CorrectAnswer { get { return this.correctAnswer; } }
        public string[] Freetrivia { get { return this.freeTrivia; } }
    }
}

And a TriviaForm class (TriviaForm.cs) to display the data from the Trivia class in the textboxes.

namespace D4
{
    public partial class TriviaForm : Form
    {
        //// Fields
        private Trivia trivia;
        private Question question;
        private string questionText;
        private string[] answers = new string[4];
        private Question[] data;
        private const int wumpusMaxQuestions = 5;
        private const int hazardAndPurchaseMaxQuestions = 3;


        // Replace these later after finished connecting all objects.
        private bool hazardAndPurchase = false;
        private bool wumpus = false;

        public TriviaForm()
        {
            InitializeComponent();

            this.trivia = new Trivia();
            this.data = trivia.ListOfData().ToArray();
            this.questionText = data[0].ToString();
            for (int i = 0; i < 4; i++)
                this.answers[i] = data[i].ToString();

            this.question = new Question(questionText, answers);

            QuestionOutputter();
        }

        // Displays the questions and answers on the form.
        private void QuestionOutputter()
        {
            this.txtQuestion.Text = question.QuestionText;
            this.txtBoxA.Text = question.Answers[0];
            this.txtBoxB.Text = question.Answers[1];
            this.txtBoxC.Text = question.Answers[2];
            this.txtBoxD.Text = question.Answers[3];
        }
    }
}

Okay sorry for the bad question guys, but I finally figured it out. The problem I was having was with the Question class giving an error. This was due to the fact that I was passing a null to it with my questionText and answers variables in the TriviaForm class. The code that I had:

this.data = trivia.ListOfData().ToArray();
        this.questionText = data[0].ToString();
        for (int i = 0; i < 4; i++)
            this.answers[i] = data[i].ToString();

was completely wrong. Apparently all I needed was to do a foreach loop instead. My solution:

this.data = trivia.ListOfData().ToArray();
foreach (Question q in this.data)
        {
            this.questionText = q.QuestionText.ToString();
            for (int i = 0; i < 4; i++)
                this.answers[i] = q.Answers[i];
            this.question = new Question(questionText, answers);
        }

I tried this out and it worked out really well! Sorry for wasting your time.

Add a radio button next to each of the answer text boxes and add event handlers to those radio buttons to evaluate whether the selected answer is the correct one.


Something like this may work with your existing code, assuming you put radioButton2 next to txtBoxB, and radioButton3 next to txtBoxC etc:

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        if (((System.Windows.Forms.RadioButton)sender).Text.Substring(11,1) = this.Question.correctAnswer + 1) {
            CorrectAnswer_DoSomethingHere();
        } else {
            Incorrect_DoSomethingElse();   
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM