0

I am trying to create a program to randomly generate a name from a cake rota array. I cannot seem to get it to work. Currently, the message box shows a random number rather than the name stored in the array. Help please!

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim strNames(18) As String
        Dim frances, emma, piotr, jake, jess, jonah, john, flynn, will, nick As String

        strNames(0) = "frances"
        strNames(1) = "emma"
        strNames(2) = "piotr"
        strNames(3) = "jake"
        strNames(4) = "jess"
        strNames(5) = "jonah"
        strNames(6) = "john"
        strNames(7) = "flynn"
        strNames(8) = "will"
        strNames(9) = "nick"


        Dim rnum As Integer
        Dim temp As String
        For i = 0 To 18
            strNames(i) = i
        Next i

        Randomize()
        For i = 0 To 9
            rnum = Int(Rnd() * (UBound(strNames) - LBound(strNames) + 1) + LBound(strNames))
            temp = strNames(i)
            strNames(i) = strNames(rnum)
            strNames(rnum) = temp
        Next i


        For i = 0 To 9
            MessageBox.Show(strNames(i))
        Next i

    End Sub
1

1 Answer 1

2

The line

    For i = 0 To 18
        strNames(i) = i
    Next i

is setting the values of strNames to a number.

Taken out and it appears to work

    Dim strNames(18) As String
    Dim frances, emma, piotr, jake, jess, jonah, john, flynn, will, nick As String
    frances = "frances"
    emma = "emma"
    piotr = "piotr"
    jake = "jake"
    jess = "jess"
    jonah = "jonah"
    john = "john"
    flynn = "flynn"
    will = "will"
    nick = "nick"
    strNames(0) = frances
    strNames(1) = emma
    strNames(2) = piotr
    strNames(3) = jake
    strNames(4) = jess
    strNames(5) = jonah
    strNames(6) = john
    strNames(7) = flynn
    strNames(8) = will
    strNames(9) = nick


    Dim rnum As Integer
    Dim temp As String


    Randomize()
    For i = 0 To 9
        rnum = Int(Rnd() * (UBound(strNames) - LBound(strNames) + 1) + LBound(strNames))
        temp = strNames(i)
        strNames(i) = strNames(rnum)
        strNames(rnum) = temp
    Next i


    For i = 0 To 9
        MessageBox.Show(strNames(i))
    Next i
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.