"random addition of names in a listbox" not working

ananas

Member
Joined
May 18, 2009
Messages
6
Programming Experience
Beginner
hello
i am new to dot net and i have a problem with this code below. what is expected is that the method should add the names in the string in random order into the listbox. but the output shows only single alphabets added rather than the whole name. i don't know where im wrong. can anyone correct me?

VB.NET:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim fr(4) As String
        fr(0) = "abba"
        fr(1) = "dama"
        fr(2) = "beta"
        fr(3) = "elep"
        fr(4) = "cat"
        addrand(fr)
    End Sub

Sub addrand(ByVal rand() As String)
        Dim nofr As Integer = rand.Length - 1
        Dim n As String
        Dim Random As New System.Random()
        For Each n In rand
            Dim m As Integer = Random.Next(nofr)
            ListBox3.Items.Add(n(m))
        Next
    End Sub
 
With "String(n)" you select the Char at position "n" in the string.

Do something like

dim r as new random
dim tmp as string
' mix up the array
for i as integer = 0 to arr.length - 2
n = r.next(i, arr.length)
tmp = arr(i)
arr(i) = arr(n)
arr(n) = tmp
next i
' fill listbox
for i as integer = 0 to arr.length-1
listbox1.items.add(arr(i))
next i
 
found the mistake

yeah , thx for the replies. it sure did help me. and i found what mistake i made in the code
VB.NET:
Sub addrand(ByVal rand() As String)

        Array.Sort(rand)
        Dim nofr As Integer = rand.Length
        Dim n As String
        Dim Random As New System.Random()

        For Each n In rand
            Dim index As Integer = Random.Next(nofr)
            ListBox3.Items.Add([COLOR="Red"]rand[/COLOR](index))
        Next
end sub

and now it adds in random not just the alphabets but the whole word itself.
thx again for the help.:)
 
Back
Top