I have declared a dictionary right at the start of my program as such
我已经在我的程序开始时声明了一本词典
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Dictionary<string, int> dictionary = new Dictionary<string, int>();
}
and I have a function that fills the dictionary using a string it is sent
我有一个函数,使用它发送的字符串填充字典
public IDictionary<string, int> SortTextIntoDictionary(string text)
{
text = text.Replace(",", ""); //Just cleaning up a bit
text = text.Replace(".", ""); //Just cleaning up a bit
text = text.Replace(Environment.NewLine, " ");
string[] arr = text.Split(' '); //Create an array of words
foreach (string word in arr) //let's loop over the words
{
if (dictionary.ContainsKey(word)) //if it's in the dictionary
dictionary[word] = dictionary[word] + 1; //Increment the count
else
dictionary[word] = 1; //put it in the dictionary with a count 1
}
return(dictionary);
}
However my functions aren't seeing the dictionary that I created at the start and I do not know how to return a dictionary from a function. I have tried declaring my dictionary static and/or public and such but I just get more errors.
但是我的函数没有看到我在开始时创建的字典,我不知道如何从函数返回字典。我试过声明我的字典静态和/或公共等等但我只是得到更多的错误。
1 个解决方案
#1
Declare your dictionary at class level:
在课程级别声明你的字典:
public partial class Form1 : Form
{
public Dictionary<string, int> dictionary = new Dictionary<string, int>();
public Form1()
{
InitializeComponent();
}
}
#1
Declare your dictionary at class level:
在课程级别声明你的字典:
public partial class Form1 : Form
{
public Dictionary<string, int> dictionary = new Dictionary<string, int>();
public Form1()
{
InitializeComponent();
}
}