KSR5
KSR5

Reputation: 81

How do I randomize an array in Visual Basic 2010?

I have any array of questions like this:

Dim Questions(25) As TheQuestions

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"

    Questions(2).Question = "What's the adjective in this sentence: 'Karen is a graceful dancer'"
    Questions(2).option1 = "Is"
    Questions(2).option2 = "Graceful"
    Questions(2).option3 = "Dancer"
    Questions(2).option4 = "Tanya"
    Questions(2).Answer = "B"
    ...

I have found a way of randomizing the question successfully, but could I make sure that the correct, four potential answers are displayed along with the question being displayed?

Below is the code for calling the Function which gets the question and then displays it in a label (lblQuestion) and where the code I am looking for needs to go, I am guessing:

Function GetQuestion(ByVal intQuestion As Integer)

    tmrOne.Start()

        If questionNumber < 25 Then
            lblQuestionNumber.Text = "Question" & " " & questionNumber
            lblQuestion.Text = Questions(intQuestion).Question
            btnAnswerA.Text = Questions(intQuestion).option1
            btnAnswerB.Text = Questions(intQuestion).option2
            btnAnswerC.Text = Questions(intQuestion).option3
            btnAnswerD.Text = Questions(intQuestion).option4
            strAnswer = Questions(intQuestion).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 used this:

lblQuestion.Text = Questions(random.Next(25)).Question

It randomizes the question, but how do I get it so that the four possible answers are shown with the correct question, like in the array above where there are FOUR options.

Many thanks!

Upvotes: 0

Views: 2007

Answers (1)

dcaswell
dcaswell

Reputation: 3167

Instead of:

lblQuestion.Text = Questions(random.Next(25)).Question

I think you just need to save the random number and do it like this:

dim questionChosen as int

questionChosen = random.Next(25)
lblQuestion.Text = Questions(questionChosen).Question

Then you update the rest of the fields using Questions(questionChosen).WhateverYouNeed

Upvotes: 1

Related Questions