Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42731

VS 2010 Having problems populating a list(of string)

$
0
0
I'm making a hangman game and I'm working on the difficulty feature. I keep getting an error when trying to select a random word. What I'm doing is populating an array at my form load here:
vb.net Code:
  1. wordList = My.Resources.wordList.Split(CChar(Environment.NewLine))

Then I choose from three buttons to set the difficulty. 0 is easy 1 is medium 2 is hard. Then I create a temporary list(of string) and add words from wordList to this new temporary list based on the difficult. I'm doing that like this:

vb.net Code:
  1. 'Create a temporary list
  2.         Dim tempList As New List(Of String)
  3.  
  4.         'Add to the temporary list based on the difficulty
  5.         For i As Integer = wordList.Count - 1 To 0 Step -1
  6.             Select Case difficult
  7.                 Case 0
  8.                     If wordList(i).Length <= 3 Then
  9.                         tempList.Add(wordList(i))
  10.                     End If
  11.                 Case 1
  12.                     If wordList(i).Length >= 4 AndAlso wordList(i).Length <= 5 Then
  13.                         tempList.Add(wordList(i))
  14.                     End If
  15.  
  16.                 Case 2
  17.                     If wordList(i).Length >= 6 Then
  18.                         tempList.Add(wordList(i))
  19.                     End If
  20.             End Select
  21.         Next

Finally I choose a random word from this newely populated temporary list of string like this:
vb.net Code:
  1. 'Choose a randomword
  2.         Dim r As New Random
  3.         Dim int As Integer = r.Next(0, tempList.Count - 1)
  4.         rndWord = tempList.Item(int)

I'm getting the error at setting int. It's telling me 'minValue' cannot be greater than maxValue. Which tells me that there must be 0 words in the temporary list. But I know that there are words in the tempList because when I add:
Code:

For i As Integer = tempList.Count - 1 To 0 Step -1
            Dim str As String = tempList.Item(i)
        Next

And set a breakpoint at the for, it tells me the count - 1 is above 0. So I don't know why it's throwing that error. Could y'all possibly point it out for me?

Viewing all articles
Browse latest Browse all 42731

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>