Marcello Vecchio
Marcello Vecchio

Reputation: 11

Error getting list from class

I've created this class:

class Riconoscimento
{
    private List<Word> words = new List<Word>();


    public List<Word> GetList()
    {
        return words;
    }

    public void loadWords()
    {
        string[] lines = File.ReadAllLines(Environment.CurrentDirectory + "/../../words.txt");
            foreach (string line in lines)
            {
                // skip commentblocks and empty lines..
                if (line.StartsWith("--") || line == String.Empty) continue;

                // split the line
                var parts = line.Split(new char[] { '|' });

                // add commandItem to the list for later lookup or execution
                words.Add(new Word() { Text = parts[0], AttachedText = parts[1], IsShellCommand = (parts[2]) });


            }
      }
}

But after loading loadWords() when I try to get the words from the class in my MainForm with

 public void engine_WordsRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        Riconoscimento _riconoscimento = new Riconoscimento();
        List<Word> words = _riconoscimento.GetList();
        var cmd = words.Where(c => c.Text == e.Result.Text).First();
}

This error occour: System.InvalidOperationException -Sequence does not contain elements.

It's like it can't retrieve the words from the class, I can't understand why. If I don't use the class but put everything in my main code it works. What should I do?

Problem solved: I loaded loadWords() in my other void, I had to load it inside my other void.

Upvotes: 1

Views: 82

Answers (3)

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19149

You dont call loadWords. you just get a empty list from this property.

public List<Word> GetList()
{
    return words;
}

you have to call loadWords before getting property.

_riconoscimento.loadWords();

Or call it from inside property.

private List<Word> words = null; // initialize new list list inside loadWords

public List<Word> GetList()
{
    if(words == null) loadWords();
    return words;
}

Upvotes: 1

D Stanley
D Stanley

Reputation: 152491

That error is coming form the call to First. You are not calling loadWords after creating the Riconoscimento, so the list never gets populated. If you have another instance somewhere that already has the data loaded, then you may be able to use it, or you could also make words static if it will be the same for every instance.

Also, if the list may not contain the value you're looking for then FirstOrDefault will return null rather than throwing an exception.

Upvotes: 1

user47589
user47589

Reputation:

You aren't calling loadWords(). That's why nothing is being loaded.

public void engine_WordsRecognized(object sender, SpeechRecognizedEventArgs e)
{
    Riconoscimento _riconoscimento = new Riconoscimento();
    _riconoscimento.loadWords();
    List<Word> words = _riconoscimento.GetList();
    var cmd = words.Where(c => c.Text == e.Result.Text).First();
}

Upvotes: 2

Related Questions