I’m trying to design a simple program to pick random questions from an array, unfortunately however I have no idea on how to limit how many programs are chosen. What I would like to do, is after five questions have been given, is for a message box to pop up and say that no more questions can be given.
Also, to keep the same questions from reappearing, what code do I use to remove the question from the array? I already know it would be something like
question.removeat("question here")
However since I’m using an array list I can’t use something like
For 1 to 9…
because when the question is removed it would result back in an error. I’ve tried this however
For ubound to lbound…
but to no success. Also, as you can probably imagine, I’m kind of new at this, so some examples would help along with the explanation.
I typed this in, and it didn’t quite work out. Any suggestions?
Counter = 0
If Counter < 2 Then
Randomize()
For i = 1 To 6
j = Int(Rnd() * i)
lblQ.Text = questions(j)
Next
Counter = Counter + 1
ElseIf Counter = 2 Then
MsgBox("Restart Program")
End If
Use a dictionary to store the questions. The key can be the question number, the entry can be a class that has the question and an "already asked" boolean.
When you ask a question, mark it as already asked. You’ll be able to filter the list for alreadyAsked=False using LINQ, that will keep only unasked questions in the list.
Public Class MyQuestionsClass
Public Question as String
Public AlreadyAsked as Boolean = False
Public Sub New(ByVal inQuestion as String)
Me.Question=inQuestion
Me.AlreadyAsked=False
End Sub
End Class
Dim myQuestions as new Dictionary(Of Integer, MyQuestionsClass)
For q = 1 to 10
myQuestions.add(q, new MyQuestionsClass("Question #" & q)
Next
Then let’s say you ask question #4
myQuestions(4).AlreadyAsked = True
If you need more help you can email me.