gpt4 book ai didi

c# - 从类(class)获取列表时出错

转载 作者:太空宇宙 更新时间:2023-11-03 21:16:49 24 4
gpt4 key购买 nike

我创建了这个类:

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]) });


}
}
}

但是在加载 loadWords() 之后,当我尝试使用 MainForm 中的类获取单词时

 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();
}

此错误发生: System.InvalidOperationException - 序列不包含元素。

好像无法从类中检索单词,我不明白为什么。如果我不使用该类,而是将所有内容都放在我的主代码中,它就可以工作。我该怎么办?

问题已解决:我在我的另一个空隙中加载了 loadWords(),我不得不将它加载到我的另一个空隙中。

最佳答案

您没有调用 loadWords()。这就是没有加载任何内容的原因。

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();
}

关于c# - 从类(class)获取列表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33639438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com