Question Loop Thru Functions

inkedgfx

Well-known member
Joined
Sep 29, 2012
Messages
139
Location
USA
Programming Experience
Beginner
I am writting a Trivia Game.....there will be 12 catagories , each catagory will have at least 100 question.....I will create 10 different functions for each catagory to randomly pick a question from a list of 10 questions.....is it poosible to loop thru the 10 functions and randomly select a function to run when the user clicks the next buttoon?

Thank You for your help

InkedGFX
 
this function is called in a button click event...everytime the user clicks the button this function is fired......so some of the questions are repeated....I dont want that...I thought I could figure out how to use a Dictionary...but they are mor difficult to learn than a list.......or an arrayList....the above function doesnt work they way it is now....I will either change it back to a List or hopefully learn the Dictionary........


InkedGFX
 
Hi,

The reason that this is happening is because you are creating your list of questions each time you access this function. The way to preserve the list of questions is to do this sort of thing on a Form Load Event which then brings into play the consideration of the scope of your variables.

I can see that there is some confusion going on here so to demonstrate the use of a Dictionary, scope of variables, using the Random class etc etc, I have written a quick Quiz project for you. There are loads of comments for you to get your head round what is going on and hopefully you can use this as a basis to complete your own project. See here:-

VB.NET:
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

Hope that helps.

Cheers,

Ian
 
wow....I really appreciate you taking the time to help me understand this...ans taking the time to write this our for me....I will have to study this and try to wrap my head around it......

I will let you know how it is going once I get it worked out.....

Thanks Again

InkedGFX
 
lol..so if im reading this correctly..I will add the quizQuestions to the QuizQuestions Class?

InkedGFX
 
Hi inkedgfx,

No you have not! There is no rush when getting your head round something new for the first time. Just take your time and take each comment and code statement in turn and see how it interacts with the project logic and you will eventually see how it all comes together.

By all means, ask any questions you need if you are struggling with any particular aspect of the project.

Good luck and cheers,

Ian
 
I will take my time and study this code block...I'm sure I will have more (newbie) questions........


InkedGFX
 
I know i'm a pita but I dont understand where to add the questions...or actually how to add the questions.......

InkedGFX
 
Hi,

To get started you need to follow the comments made in the Form Load event. i.e:-

VB.NET:
'if you were creating your questions for real then you would do something like:-
'notice that the correct answer is an integer. This is due to the fact that the
'correct answer points to the correct answer already specified in the list of
'available answers
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

If I were doing this I would first of all keep the Form Load event Clean and Tidy by calling a CreateMyQuizQuestions subroutine from the Form Load event. i.e:-

VB.NET:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  CreateMyQuizQuestions()
End Sub

I would then create the subroutine called CreateMyQuizQuestions. This is where we would then create the questions for the quiz. i.e:-

VB.NET:
Private Sub CreateMyQuizQuestions()
  Dim myQuizQuestion1 As New QuizQuestion
  With myQuizQuestion1
    .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
  AddQuestionToDictionary(QuizCategories.History, myQuizQuestion1)
 
  Dim myQuizQuestion2 As New QuizQuestion
  With myQuizQuestion2
    .Question = "This is the second real question to be added to the dictionary"
    .AvailableAnswers.Add("Real Second Answer 1")
    .AvailableAnswers.Add("Real Second Answer 2")
    .AvailableAnswers.Add("Real Second Answer 3")
    .CorrectAnswer = 0
  End With
  AddQuestionToDictionary(QuizCategories.Science, myQuizQuestion2)
 
'etc
'etc
'etc
End Sub

Now that we have created the questions, and as per my comments, we need to create a subroutine to add the question to the Dictionary. Lets call this routine AddQuestionToDictionary which takes two parameters. The first being the Category where the question should be placed and the second is the question itself. As you can see above we then call this subroutine after each question to add it to the Dictionary passing both the Category and the Question as parameters. i.e:-

VB.NET:
Private Sub AddQuestionToDictionary(ByVal SelectedCategory As QuizCategories, ByVal NewQuizQuestion As QuizQuestion)
  If myQuestions.ContainsKey(SelectedCategory) Then
    myQuestions(SelectedCategory).Add(NewQuizQuestion)
  Else
    myQuestions(SelectedCategory) = {NewQuizQuestion}.ToList
  End If
End Sub

That's it. Hope that helps.

Cheers,

Ian
 
Again Thank You for the help....it seems I am totally lost ......i cleaned up the form load event and took your advice and added the sub "AddQuestionToDictionary" but i'm getting an error at the last line

Public Sub AddQuestionToDictionary(ByVal SelectedCatagory As TriviaCatagories, ByVal NewQuizQuestion As QuizQuestion)
        If myQuestions.ContainsKey(SelectedCatagory) Then
            myQuestions(SelectedCatagory).Add(NewQuizQuestion)
        Else
            myQuestions(SelectedCatagory) = {NewQuizQuestion}.tolist <-------- error is "tolist" isnt a member of "System.Array"
        End If
    End Sub



thank you

InkedGFX
 
 Public myQuestions As New Dictionary(Of TriviaCatagories, List(Of QuizQuestion))


this is what I have....

InkedGFX
 
Back
Top