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
 
Why would you need 10 functions? If you want to choose 10 questions then you only need one function to do it. You would then put those 10 questions into an array or an appropriate collection.
 
Many it might be a good choosing to put questions string in an arraylist then pick up them randomly from arrraylist.
 
Why would you need 10 functions? If you want to choose 10 questions then you only need one function to do it. You would then put those 10 questions into an array or an appropriate collection.

I wrote a function that has 10 questions in it....those questions are in a list(of string) which works fine...the problem is I dont really know how to pick a random question then remove it from the list so it doesnt get picked again.

 Public Function American_Question_2(ByVal lblQuestion As Label, ByVal lblAnswer As Label, ByVal rb1 As RadioButton, ByVal rb2 As RadioButton, ByVal rb3 As RadioButton)
        Dim rndQuestion As New Random
        Dim Questions As New List(Of String)
        Try
start:      ' add the questions to a list 
            Questions.Add("What aptly named village has the highest post office in the United States?")
            Questions.Add("What newspaper, launched in 1982, was dubbed the McPaper because it provided its readers with [McNuggets] of news?")
            Questions.Add("How fast--in words per minute--does the average American adult read?")
            Questions.Add("Which two states have neighboring towns named for explorers Meriwether Lewis and William Clark?")
            Questions.Add("Which of the 50 states takes in the least amount of tourist dollars?")
            Questions.Add("What is the name of the boulevard on which Fort Knox is located?")
            Questions.Add("What are roller coasters classified as by the U.S. Patent Office?")
            Questions.Add("Which of the states uses the Napoleonic code rather than English common law as the basis for its civil law?")
            Questions.Add("What major vegetable crop was grown in Beverly Hills, California, before it became home to the rich and famous?")
            Questions.Add("What is the only place below sea level in the United States that is not in the California desert? Hint: It's a major city.")

            Randomize() ' randomize the list of questions
            lblQuestion.Text = Questions.Item(Int(Rnd() * Questions.Count - 1)) ' put the random question to a label
            ' set the radio buttons with three multiple chioce answers
            If lblQuestion.Text = "What aptly named village has the highest post office in the United States?" Then
                rb1.Text = "Climax, Colorado"
                rb2.Text = "Berlington, Vermont"
                rb3.Text = "Mile High City, Denver"
            ElseIf lblQuestion.Text = "What newspaper, launched in 1982, was dubbed the McPaper because it provided its readers with [McNuggets] of news?" Then
                rb1.Text = "USA Today"
                rb2.Text = "New York Post"
                rb3.Text = "Daily Gazzette"
            ElseIf lblQuestion.Text = "How fast--in words per minute--does the average American adult read?" Then
                rb1.Text = "800"
                rb2.Text = "275"
                rb3.Text = "150"
            ElseIf lblQuestion.Text = "Which two states have neighboring towns named for explorers Meriwether Lewis and William Clark?" Then
                rb1.Text = "New Jersey and Maryland"
                rb2.Text = "Massachusetts and Conneticut"
                rb3.Text = "Idaho and Washington"
            ElseIf lblQuestion.Text = "Which of the 50 states takes in the least amount of tourist dollars?" Then
                rb1.Text = "California"
                rb2.Text = "Rhode Island"
                rb3.Text = "Luisiana"
            ElseIf lblQuestion.Text = "What is the name of the boulevard on which Fort Knox is located?" Then
                rb1.Text = "Golden Blvd"
                rb2.Text = "SafeWay Blvd"
                rb3.Text = "Bullion Boulevard"
            ElseIf lblQuestion.Text = "What are roller coasters classified as by the U.S. Patent Office?" Then
                rb1.Text = "Scenic RailWays"
                rb2.Text = "Metal Twist's"
                rb3.Text = "Looptie Loops"
            ElseIf lblQuestion.Text = "Which of the states uses the Napoleonic code rather than English common law as the basis for its civil law?" Then
                rb1.Text = "Louisiana"
                rb2.Text = "Texas"
                rb3.Text = "Maine"
            ElseIf lblQuestion.Text = "What major vegetable crop was grown in Beverly Hills, California, before it became home to the rich and famous?" Then
                rb1.Text = "Lima Beans"
                rb2.Text = "carrots"
                rb3.Text = "Peas"
            ElseIf lblQuestion.Text = "What is the only place below sea level in the United States that is not in the California desert?  - Hint: It's a major city -" Then
                rb1.Text = "New York City"
                rb2.Text = "New Orleans"
                rb3.Text = "San Fransico"
            End If
        Catch ex As Exception
            GoTo start 'if we get an outofrange error then go to the beginning and draw again

        End Try
 

        Return Questions
    End Function


how could I remove the question that was picked from the list..?

also this function is in a button called "next" so when the user answers the question they need to click the " next" button to get another question.....will the question repeat if the same function is called ?

thank you for your help.

InkedGFX
 
Maybe you can add a specific char each string line's prefex after which one choosed by randomly and then the choosed one never choose again with your else if statement. You can used Insert() function for this. What do you think?
 
I'm not sure how to do that.....

InkedGFX
I am not sure that I could not find a chance to try your code but hope this would work.
[/FONT]    Public Function American_Question_2(ByVal lblQuestion As Label, ByVal lblAnswer As Label, ByVal rb1 As RadioButton, ByVal rb2 As RadioButton, ByVal rb3 As RadioButton)
        Dim rndQuestion As New Random
        Dim Questions As New List(Of String)
        Dim TakeValueRandom As Integer
        Try
start:      ' add the questions to a list
            Questions.Add("What aptly named village has the highest post office in the United States?")
            Questions.Add("What newspaper, launched in 1982, was dubbed the McPaper because it provided its readers with [McNuggets] of news?")
            Questions.Add("How fast--in words per minute--does the average American adult read?")
            Questions.Add("Which two states have neighboring towns named for explorers Meriwether Lewis and William Clark?")
            Questions.Add("Which of the 50 states takes in the least amount of tourist dollars?")
            Questions.Add("What is the name of the boulevard on which Fort Knox is located?")
            Questions.Add("What are roller coasters classified as by the U.S. Patent Office?")
            Questions.Add("Which of the states uses the Napoleonic code rather than English common law as the basis for its civil law?")
            Questions.Add("What major vegetable crop was grown in Beverly Hills, California, before it became home to the rich and famous?")
            Questions.Add("What is the only place below sea level in the United States that is not in the California desert? Hint: It's a major city.")

            Randomize() ' randomize the list of questions
            TakeValueRandom = Int(Rnd() * Questions.Count - 1)
            lblQuestion.Text = Questions.Item(TakeValueRandom) ' put the random question to a label
            ' set the radio buttons with three multiple chioce answers
            If lblQuestion.Text = "What aptly named village has the highest post office in the United States?" Then
                rb1.Text = "Climax, Colorado"
                rb2.Text = "Berlington, Vermont"
                rb3.Text = "Mile High City, Denver"
            ElseIf lblQuestion.Text = "What newspaper, launched in 1982, was dubbed the McPaper because it provided its readers with [McNuggets] of news?" Then
                rb1.Text = "USA Today"
                rb2.Text = "New York Post"
                rb3.Text = "Daily Gazzette"
            ElseIf lblQuestion.Text = "How fast--in words per minute--does the average American adult read?" Then
                rb1.Text = "800"
                rb2.Text = "275"
                rb3.Text = "150"
            ElseIf lblQuestion.Text = "Which two states have neighboring towns named for explorers Meriwether Lewis and William Clark?" Then
                rb1.Text = "New Jersey and Maryland"
                rb2.Text = "Massachusetts and Conneticut"
                rb3.Text = "Idaho and Washington"
            ElseIf lblQuestion.Text = "Which of the 50 states takes in the least amount of tourist dollars?" Then
                rb1.Text = "California"
                rb2.Text = "Rhode Island"
                rb3.Text = "Luisiana"
            ElseIf lblQuestion.Text = "What is the name of the boulevard on which Fort Knox is located?" Then
                rb1.Text = "Golden Blvd"
                rb2.Text = "SafeWay Blvd"
                rb3.Text = "Bullion Boulevard"
            ElseIf lblQuestion.Text = "What are roller coasters classified as by the U.S. Patent Office?" Then
                rb1.Text = "Scenic RailWays"
                rb2.Text = "Metal Twist's"
                rb3.Text = "Looptie Loops"
            ElseIf lblQuestion.Text = "Which of the states uses the Napoleonic code rather than English common law as the basis for its civil law?" Then
                rb1.Text = "Louisiana"
                rb2.Text = "Texas"
                rb3.Text = "Maine"
            ElseIf lblQuestion.Text = "What major vegetable crop was grown in Beverly Hills, California, before it became home to the rich and famous?" Then
                rb1.Text = "Lima Beans"
                rb2.Text = "carrots"
                rb3.Text = "Peas"
            ElseIf lblQuestion.Text = "What is the only place below sea level in the United States that is not in the California desert?  - Hint: It's a major city -" Then
                rb1.Text = "New York City"
                rb2.Text = "New Orleans"
                rb3.Text = "San Fransico"
            End If
            Dim babos As String = lblQuestion.Text
            Questions.Item(TakeValueRandom) = babos.Insert(0, "@")
        Catch ex As Exception
            GoTo start 'if we get an outofrange error then go to the beginning and draw again
        End Try
        Return Questions
    End Function[FONT=comic sans ms]
 
Hi,

Have a look at a post made by jmcilhinney some time back. It will teach you a few things, such as using the random class but also how you can return a unique random element from within a list. This will be a good starting point for you to get going.

Unique, Random Selections from a List-VBForums

In addition to that, I would suggest having a look at Dictionary's to hold your questions and answers so that you do not have to do what you are doing with your if statements. A general rule of thumb in programming is that if you create a literal, say, "This is some literal text in my program" and then you type this literal again somewhere then you are doing it wrong.

Hope that helps.

Cheers,

Ian
 
Im not sure if I follow.... I have created a list ...then generate a random question from the list...but how do I know what question was generated and how would I remove it ?

thank you for the help

InkedGFX
 
I am not sure that I could not find a chance to try your code but hope this would work.
    Public Function American_Question_2(ByVal lblQuestion As Label, ByVal lblAnswer As Label, ByVal rb1 As RadioButton, ByVal rb2 As RadioButton, ByVal rb3 As RadioButton)
        Dim rndQuestion As New Random
        Dim Questions As New List(Of String)
        Dim TakeValueRandom As Integer
        Try
start:      ' add the questions to a list
            Questions.Add("What aptly named village has the highest post office in the United States?")
            Questions.Add("What newspaper, launched in 1982, was dubbed the McPaper because it provided its readers with [McNuggets] of news?")
            Questions.Add("How fast--in words per minute--does the average American adult read?")
            Questions.Add("Which two states have neighboring towns named for explorers Meriwether Lewis and William Clark?")
            Questions.Add("Which of the 50 states takes in the least amount of tourist dollars?")
            Questions.Add("What is the name of the boulevard on which Fort Knox is located?")
            Questions.Add("What are roller coasters classified as by the U.S. Patent Office?")
            Questions.Add("Which of the states uses the Napoleonic code rather than English common law as the basis for its civil law?")
            Questions.Add("What major vegetable crop was grown in Beverly Hills, California, before it became home to the rich and famous?")
            Questions.Add("What is the only place below sea level in the United States that is not in the California desert? Hint: It's a major city.")

            Randomize() ' randomize the list of questions
            TakeValueRandom = Int(Rnd() * Questions.Count - 1)
            lblQuestion.Text = Questions.Item(TakeValueRandom) ' put the random question to a label
            ' set the radio buttons with three multiple chioce answers
            If lblQuestion.Text = "What aptly named village has the highest post office in the United States?" Then
                rb1.Text = "Climax, Colorado"
                rb2.Text = "Berlington, Vermont"
                rb3.Text = "Mile High City, Denver"
            ElseIf lblQuestion.Text = "What newspaper, launched in 1982, was dubbed the McPaper because it provided its readers with [McNuggets] of news?" Then
                rb1.Text = "USA Today"
                rb2.Text = "New York Post"
                rb3.Text = "Daily Gazzette"
            ElseIf lblQuestion.Text = "How fast--in words per minute--does the average American adult read?" Then
                rb1.Text = "800"
                rb2.Text = "275"
                rb3.Text = "150"
            ElseIf lblQuestion.Text = "Which two states have neighboring towns named for explorers Meriwether Lewis and William Clark?" Then
                rb1.Text = "New Jersey and Maryland"
                rb2.Text = "Massachusetts and Conneticut"
                rb3.Text = "Idaho and Washington"
            ElseIf lblQuestion.Text = "Which of the 50 states takes in the least amount of tourist dollars?" Then
                rb1.Text = "California"
                rb2.Text = "Rhode Island"
                rb3.Text = "Luisiana"
            ElseIf lblQuestion.Text = "What is the name of the boulevard on which Fort Knox is located?" Then
                rb1.Text = "Golden Blvd"
                rb2.Text = "SafeWay Blvd"
                rb3.Text = "Bullion Boulevard"
            ElseIf lblQuestion.Text = "What are roller coasters classified as by the U.S. Patent Office?" Then
                rb1.Text = "Scenic RailWays"
                rb2.Text = "Metal Twist's"
                rb3.Text = "Looptie Loops"
            ElseIf lblQuestion.Text = "Which of the states uses the Napoleonic code rather than English common law as the basis for its civil law?" Then
                rb1.Text = "Louisiana"
                rb2.Text = "Texas"
                rb3.Text = "Maine"
            ElseIf lblQuestion.Text = "What major vegetable crop was grown in Beverly Hills, California, before it became home to the rich and famous?" Then
                rb1.Text = "Lima Beans"
                rb2.Text = "carrots"
                rb3.Text = "Peas"
            ElseIf lblQuestion.Text = "What is the only place below sea level in the United States that is not in the California desert?  - Hint: It's a major city -" Then
                rb1.Text = "New York City"
                rb2.Text = "New Orleans"
                rb3.Text = "San Fransico"
            End If
            Dim babos As String = lblQuestion.Text
            Questions.Item(TakeValueRandom) = babos.Insert(0, "@")
        Catch ex As Exception
            GoTo start 'if we get an outofrange error then go to the beginning and draw again
        End Try
        Return Questions
    End Function[FONT=comic sans ms][/FONT]

could you explain what you have added to the code? im not understanding what this will do.....btw - thank you for your help.

InkedGFX
 
TakeValueRandom is an integer and it holds random() value when its execute for understanding which one choosed by random function exactly and then we can use insert funtion to change Questions' items with random's integer value. The insert function inserts @ char at the beginning of the Question's item so that it would not choose again by control of your if statements. This method is a temporary one and it in needs to remove function to remove @ char after all. IanRyder offered you a great method, you better to use that method for own your program good.
 
I am researching the Dictionary in vb.net as we speak....... Im also having a problem removing the question picked , so the same question doesnt get picked again....I can remove the question but if the function gets called again the list is populated all over again with the same questions..so the same question can be generated again and again...even if it is getting removed when the function is called the first time.

thank You for your help.

InkedGFX
 
You look need multidimentional array for main strings and radiobuttons text with IanRyder's offering. But your all lines of the code will change. Can you make it?
 
I dont understand.....I changed my code to a Dictionary (of String, string) but cant get the function to load the label with a random question..at least with a List I could get it to work....

 Public Function American_Question_2(ByVal lblQuestion As Label, ByVal lblAnswer As Label, ByVal rb1 As RadioButton, ByVal rb2 As RadioButton, ByVal rb3 As RadioButton)
        Dim rndQuestion As New Random
        'Dim Questions As New List(Of String)
        Dim Questions As Dictionary(Of String, String) = New Dictionary(Of String, String)
        Dim picked As String
        Try
start:      ' add the questions to a list 
            Questions.Add("Question1", "What aptly named village has the highest post office in the United States?")
            Questions.Add("Question2", "What newspaper, launched in 1982, was dubbed the McPaper because it provided its readers with [McNuggets] of news?")
            Questions.Add("Question3", "How fast--in words per minute--does the average American adult read?")
            Questions.Add("Question4", "Which two states have neighboring towns named for explorers Meriwether Lewis and William Clark?")
            Questions.Add("Question5", "Which of the 50 states takes in the least amount of tourist dollars?")
            Questions.Add("Question6", "What is the name of the boulevard on which Fort Knox is located?")
            Questions.Add("Question7", "What are roller coasters classified as by the U.S. Patent Office?")
            Questions.Add("Question8", "Which of the states uses the Napoleonic code rather than English common law as the basis for its civil law?")
            Questions.Add("Question9", "What major vegetable crop was grown in Beverly Hills, California, before it became home to the rich and famous?")
            Questions.Add("Question10", "What is the only place below sea level in the United States that is not in the California desert? Hint: It's a major city.")

            ' Randomize() ' randomize the list of questions
            picked = Questions.Item(Int(Rnd() * Questions.Count - 1)) ' put the random question to a label
            lblQuestion.Text = picked
          
            ' set the radio buttons with three multiple chioce answers
            If lblQuestion.Text = "What aptly named village has the highest post office in the United States?" Then
                rb1.Text = "Climax, Colorado"
                rb2.Text = "Berlington, Vermont"
                rb3.Text = "Mile High City, Denver"
            ElseIf lblQuestion.Text = "What newspaper, launched in 1982, was dubbed the McPaper because it provided its readers with [McNuggets] of news?" Then
                rb1.Text = "USA Today"
                rb2.Text = "New York Post"
                rb3.Text = "Daily Gazzette"
            ElseIf lblQuestion.Text = "How fast--in words per minute--does the average American adult read?" Then
                rb1.Text = "800"
                rb2.Text = "275"
                rb3.Text = "150"
            ElseIf lblQuestion.Text = "Which two states have neighboring towns named for explorers Meriwether Lewis and William Clark?" Then
                rb1.Text = "New Jersey and Maryland"
                rb2.Text = "Massachusetts and Conneticut"
                rb3.Text = "Idaho and Washington"
            ElseIf lblQuestion.Text = "Which of the 50 states takes in the least amount of tourist dollars?" Then
                rb1.Text = "California"
                rb2.Text = "Rhode Island"
                rb3.Text = "Luisiana"
            ElseIf lblQuestion.Text = "What is the name of the boulevard on which Fort Knox is located?" Then
                rb1.Text = "Golden Blvd"
                rb2.Text = "SafeWay Blvd"
                rb3.Text = "Bullion Boulevard"
            ElseIf lblQuestion.Text = "What are roller coasters classified as by the U.S. Patent Office?" Then
                rb1.Text = "Scenic RailWays"
                rb2.Text = "Metal Twist's"
                rb3.Text = "Looptie Loops"
            ElseIf lblQuestion.Text = "Which of the states uses the Napoleonic code rather than English common law as the basis for its civil law?" Then
                rb1.Text = "Louisiana"
                rb2.Text = "Texas"
                rb3.Text = "Maine"
            ElseIf lblQuestion.Text = "What major vegetable crop was grown in Beverly Hills, California, before it became home to the rich and famous?" Then
                rb1.Text = "Lima Beans"
                rb2.Text = "carrots"
                rb3.Text = "Peas"
            ElseIf lblQuestion.Text = "What is the only place below sea level in the United States that is not in the California desert?  - Hint: It's a major city -" Then
                rb1.Text = "New York City"
                rb2.Text = "New Orleans"
                rb3.Text = "San Fransico"
            End If
        Catch ex As Exception
            GoTo start 'if we get an outofrange error then go to the beginning and draw again

        End Try
 

        Return Questions
    End Function


InkedGFX
 
How do you call that function in runtime? Please give me an example of that function's usage.
 
Back
Top