WPFQuiz.QuestionCollection.GetRandomQuestionAtLevel C# (CSharp) Method

GetRandomQuestionAtLevel() public method

public GetRandomQuestionAtLevel ( int intLevel ) : Question
intLevel int
return Question
        public Question GetRandomQuestionAtLevel(int intLevel)
        {
            List<Question> lstQuestionPool = new List<Question>();

            foreach (Question questionCurrent in lstQuestions)
            {
                if (questionCurrent.Level == intLevel)
                {
                    lstQuestionPool.Add(questionCurrent);
                }
            }

            if (lstQuestionPool.Count == 0) {
                //MessageBox.Show("No Questions at level " + intLevel);
                Console.WriteLine("No question at that level");
                //return null;
                return GetRandomQuestionAtLevel(intLevel - 1);
            }

            int randValue = Convert.ToInt32(rngPRNG.NextDouble() * (lstQuestionPool.Count - 0) + 0);

            Question qSelected = null;

            try
            {
                qSelected = lstQuestionPool[randValue];
            }
            catch (ArgumentOutOfRangeException)
            {
                qSelected = lstQuestionPool[randValue-1];
            }

            return qSelected;
        }

Usage Example

Example #1
0
 /// <summary>
 /// Get the next question
 /// </summary>
 /// <returns>Returns a question object</returns>
 public Question nextQuestion()
 {
     return(qCollection.GetRandomQuestionAtLevel(Level));
 }