Random word from file (IO.File.ReadAllLines)

Johnson

Well-known member
Joined
Mar 6, 2009
Messages
158
Programming Experience
Beginner
Returns whole lines, the odd occasion a word. any thing obvious wrong?

VB.NET:
    Private _word As String()
    Private _i As New Random

    Private Sub btnGetNewhangManWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles btnGetNewhangManWord.Click
        If _word Is Nothing Then
            MessageBox.Show(NoFileSelected, Application.ProductName)
            Exit Sub
        End If

        Dim index As Integer = _i.Next(1, _word.Length)
        Dim word As String = _word(index)

        txtHangManSelection.Text = word
    End Sub

    Private Sub btnLoadNewHangManChoices_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles btnLoadNewHangManChoices.Click
        ' (ident) Create an instance of the open file dialog box.
        Dim ofdFileDialog As OpenFileDialog = New OpenFileDialog

        ' (ident) Set filter options and filter index.
        ofdFileDialog.Filter = FileDialogFilter
        ofdFileDialog.FilterIndex = 1

        If ofdFileDialog.ShowDialog() = DialogResult.OK Then
            ' (ident) File selected so save it's contents.
            _word = IO.File.ReadAllLines(ofdFileDialog.FileName)
            Exit Sub
        End If

        MessageBox.Show(NoFileSelected, Application.ProductName)
    End Sub

Not sure if i am being slow this early in the morning
 
Code looks sound - I guess the two places I'd look at resolving the issue would be to debug your _word array when it's filled by readlines and your source file.

We can assume the source file has a single word per line, right?
 
Back
Top