Public Class Form1
'we firstly declare an enumeration to define the Quiz categories
'we can then use these categories as the key to a dictionary so
'that we can then select random categories as we need to
Private Enum QuizCategories
Science
Space
History
ArtAndCulture
GeneralKnowlege
End Enum
'Here we calculate the total categories in the enumeration
Private TotalCategories As Integer = [Enum].GetValues(GetType(QuizCategories)).Length
'here we define the dictionary with the key being QuizCategories and the value
'of the dictionay being a List(Of QuizQuestions) - Have a look at the bottom of
'this code for the definition of the QuizQuestion class
Private myQuestions As New Dictionary(Of QuizCategories, List(Of QuizQuestion))
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'load the questions and answers into the dictionary
'these are example random questions and answers in random categories
'for the purpose of this exercise
Dim myRand As New Random
For Counter As Integer = 1 To 300
Dim myNewQuestion As New QuizQuestion
With myNewQuestion
.Question = "Quiz Question No. " & Counter
For AnsCount As Integer = 1 To 3
.AvailableAnswers.Add("Quiz Question No. " & Counter & " - Answer Option No. " & AnsCount)
Next
.CorrectAnswer = myRand.Next(0, .AvailableAnswers.Count)
End With
Dim myRandomCategory As QuizCategories = DirectCast(myRand.Next(0, TotalCategories), QuizCategories)
If myQuestions.ContainsKey(myRandomCategory) Then
myQuestions(myRandomCategory).Add(myNewQuestion)
Else
myQuestions(myRandomCategory) = {myNewQuestion}.ToList
End If
Next
'if you were creating your questions for real then would would do something like this:-
'notice the the correct answer is an integer. This is due to the fact that the
'correct answer points to the correct answer alreay specified in the list of
'available answers - remember the list is Zero indexed
Dim myRealQuestion As New QuizQuestion
With myRealQuestion
.Question = "This is a real question to be added to the dictionary"
.AvailableAnswers.Add("Real Possible Answer 1")
.AvailableAnswers.Add("Real Possible Answer 2")
.AvailableAnswers.Add("Real Possible Answer 3")
.CorrectAnswer = 1
End With
'put this next section in a subroutine so that you do not type it for every
'question that needs to be added to the dictionary passing the quiz
'category and question as parameters
If myQuestions.ContainsKey(QuizCategories.GeneralKnowlege) Then
myQuestions(QuizCategories.GeneralKnowlege).Add(myRealQuestion)
Else
myQuestions(QuizCategories.GeneralKnowlege) = {myRealQuestion}.ToList
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'here we define a new random class
Dim myRand As New Random
'now we select a random catagory for a question to be picked from
Dim myCurrentCategory As QuizCategories = DirectCast(myRand.Next(0, TotalCategories), QuizCategories)
'check to make sure there are questions left in the selected category
If myQuestions(myCurrentCategory).Count > 0 Then
'get a random index within the list of questions in the selected random category
Dim myRandomQuestionIndex As Integer = myRand.Next(0, myQuestions(myCurrentCategory).Count)
'now we use the random question index to get the correct QuizQuestion from the
'dictionary under the correct category
Dim myCurrentQuestion As QuizQuestion = DirectCast(myQuestions(myCurrentCategory).Item(myRandomQuestionIndex), QuizQuestion)
'display the selected category and Question. we then also loop through
'the possible answers for the question.
'this can be used to populate textbox's radion buttons as you need
MsgBox("Category - " & myCurrentCategory & " - " & myCurrentQuestion.Question)
For Each QuestionAnswer As String In myCurrentQuestion.AvailableAnswers
MsgBox(QuestionAnswer.ToString)
Next
'This is where we can then remove the selected random question
'from the list of questions within the dictionary
'this ensures that the question is never picked twice
myQuestions(myCurrentCategory).RemoveAt(myRandomQuestionIndex)
Else
MsgBox("No more Questions Available in this Category!")
End If
End Sub
'this is the class that has been designed to hold the questions
'that will be added to the dictionary
Private Class QuizQuestion
Public Property Question As String
Public Property AvailableAnswers As New List(Of String)
Public Property CorrectAnswer As Integer
End Class
End Class