Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to read in a text file (Words.txt) and then select a word out from the "Words.txt" file.

However I am now receiving a 'minValue' cannot be greater than maxValue error.

Can anyone help me with what I may have done wrong?

My WordListHelper class:

public class WordListHelper
    {

        private readonly List<string> _wordList = new List<string>();        

        private async void ProjectFile()
        {            
                var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                _Folder = await _Folder.GetFolderAsync("Hangman");
 
                // acquire file
            
                StorageFolder sf = KnownFolders.DocumentsLibrary;
                StorageFile file = await sf.GetFileAsync("Words.txt");
                string _wordList = await FileIO.ReadTextAsync(file);            
        }

        public string Getword()
        {
            Random r = new Random();

            int i = r.Next(0, _wordList.Count - 1);

            return _wordList.ElementAt(i);
        }

    }
}


Line of code that is throwing the error:
int i = r.Next(0, _wordList.Count - 1);


Regards,

Glen.
Posted

It seems _wordList is empty, so _wordList.Count - 1 = -1

First of all since _wordList is declared at your class level you should not prefix its use with type in the ProjectFile() method:
C#
private async void ProjectFile()
{
        var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        _Folder = await _Folder.GetFolderAsync("Hangman");

        // acquire file

        StorageFolder sf = KnownFolders.DocumentsLibrary;
        StorageFile file = await sf.GetFileAsync("Words.txt");
        string result = await FileIO.ReadTextAsync(file);
        _wordList.Add(result);
}


Then you should check for the content of _wordList before trying to use it in your GetWord() method:
C#
public string Getword()
{
    Random r = new Random();
    int i = -1;
    if (_wordList.Count > 0) {
       i = r.Next(0, _wordList.Count - 1);
    }
    return (i > -1) ? _wordList.ElementAt(i) : string.Empty;
}


Hope this helps.
 
Share this answer
 
v5
Comments
Glen Childs 9-Oct-13 9:22am    
Thanks phil.o, however when I take the string prefix out from the _wordList, It errors saying that you cannot convert type of string to list. However if you specify that _wordList is a string I am receiving an error on the last two lines: 1. int i = r.Next(0, _wordList.Count - 1); - operator '-' cannot be applied to method group and int 2.: string doesnt contain an elementat and no extension accepting a first argument of type string could be found.
phil.o 9-Oct-13 10:09am    
ReadTextAsync() returns an IAsyncOperation<string>.
See my updated answer.
Glen Childs 9-Oct-13 11:46am    
Thanks Phil.o, Im getting another error though - cannot await string. But if I take the keyword await out the error - cannot implicitly convert string to list<string>
phil.o 9-Oct-13 11:57am    
I will update my answer to try and solve that problem.
Looks like _wordList.Count equals zero.
Are you sure that _wordlist is filled with all the data needed when you call GetWord()?
 
Share this answer
 
Comments
Glen Childs 9-Oct-13 9:11am    
I thought it was the FileIO.ReadText async that read the contents of the file to the _wordList list.
lukeer 10-Oct-13 4:33am    
Sure it does. But from looking at your code, I'm not sure it is finished doing so at the time GetWord() is called.
If it is not, then _wordList may be empty, Count therefore equal zero, Count-1 equal -1 and that being less than the literal zero in r.Next(0, _wordList.Count - 1); is not supported by that method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900