Reputation: 81
I have randomized an array, so questions are shown in a different order every time that the quiz is played, but would prefer it if the questions weren't able to repeat.
Here is how my array is set up:
Function loadQuestions()
Questions(0).Question = "Which of these words are an adjective?"
Questions(0).option1 = "Dog"
Questions(0).option2 = "Beautiful"
Questions(0).option3 = "Steven"
Questions(0).option4 = "Bird"
Questions(0).Answer = "B"
Questions(1).Question = "What's the adjective in this sentence:" & vbCrLf & "'Kelly handled the breakable glasses very carefully'"
Questions(1).option1 = "Kelly"
Questions(1).option2 = "Handled"
Questions(1).option3 = "Carefully"
Questions(1).option4 = "Breakable"
Questions(1).Answer = "D"
...
This is the function that calls the questions when the quiz begins.
Function GetQuestion(ByVal intQuestion As Integer)
tmrOne.Start()
If questionNumber < 11 Then
lblQuestionNumber.Text = "Question" & " " & questionNumber
Dim questionChosen As Integer
questionChosen = random.Next(25)
lblQuestion.Text = Questions(questionChosen).Question
btnAnswerA.Text = Questions(questionChosen).option1
btnAnswerB.Text = Questions(questionChosen).option2
btnAnswerC.Text = Questions(questionChosen).option3
btnAnswerD.Text = Questions(questionChosen).option4
strAnswer = Questions(questionChosen).Answer
questionNumber = questionNumber + 1
btnAnswerA.BackColor = Color.White
btnAnswerB.BackColor = Color.White
btnAnswerC.BackColor = Color.White
btnAnswerD.BackColor = Color.White
btnAnswerA.Enabled = True
btnAnswerB.Enabled = True
btnAnswerC.Enabled = True
btnAnswerD.Enabled = True
Return intQuestion
Else
MsgBox("You have finished")
End
End If
End Function
I have tried to find something on the internet to help with this, but haven't been successful or understood it as I am new to this. I have found ArrayList.RemoveAt but not sure that is the correct syntax to use with my array?
So, how do I stop my array from repeating a question once it has already been asked? Do I put them into another array?
Any help is greatly appreciated!
Upvotes: 0
Views: 901
Reputation:
As I understand from your question, you are using an ArrayList
. In that case, yes, the RemoveAt
option sounds as the best alternative. Bear in mind that pure arrays (e.g., Dim Questions() As String
) are the most efficient type of Collection
; the main advantage of Lists
/ArrayLists
is precisely how easy is adding/removing elements, so if you use an ArrayList, better maximising one of its defining features.
The additional advantage in your specific case is that every time you remove an item, its position is filled (and the total number of items decreased). Thus, you have just to update the maximum value of your random number generator to the current number of indices (not elements, because the first index is zero).
In summary, the following changes in your code will deliver what you want:
If(Questions.Count > 0) Then
questionChosen = random.Next(0, Questions.Count - 1)
End If
And once you are done with it:
If(questionChosen >= 0 And questionChosen <= Questions.Count - 1) Then
Questions.RemoveAt(questionChosen)
End If
Upvotes: 1