[英]C# dictionary, how to get the a specific value
I have created a dictionary, and I want the user to write the city ( kommune) and then get the value (procent) displayed in a text box called txtKommuneresultalt 我已经创建了一个字典,我希望用户编写城市(公社),然后获取显示在名为txtKommuneresultalt的文本框中的值(百分比)
I am new to C#, so I hope someone can help me 我是C#的新手,所以我希望有人可以帮助我
I have tried searching for days now, nothing works.I am using Windowsforms, and have the buttonHandler ready 我已经尝试搜索几天了,但是没有用。我正在使用Windowsforms,并且已经准备好buttonHandler
this so far is my code: 到目前为止,这是我的代码:
Dictionary<double, string> dictionary = new Dictionary<double, string>();
double procent = Convert.ToDouble(txtKommuneresultat.Text);
string kommune = txtKommuneInput.Text;
dictionary.Add(11.44, "Faxe");
dictionary.Add(4.29, "Greve");
dictionary.Add(7.11, "Gulborgsund");
dictionary.Add(7.86, " Holbæk");
dictionary.Add(5.67, "Kalundborg");
dictionary.Add(4.99, "Køge");
dictionary.Add(7.28, "Lejre");
dictionary.Add(2.67, "Lolland");
dictionary.Add(4.07, "Næstved");
dictionary.Add(1.21, "Odsherred");
dictionary.Add(5.02, "Ringsted");
dictionary.Add(13.23, "Slagelse");
dictionary.Add(20.75, "Solrød");
dictionary.Add(1.81, "Sorø");
dictionary.Add(5.50, "Stevns");
dictionary.Add(1.29, "Vordingborg");
txtKommuneresultat.Show();
The first type in a dictionary is the key and the second is the value . 字典中的第一种是键 ,第二种是值 。 A dictionary allow you to look up a value based on that value's key .
字典允许你查找基于该值的键的值 。
Currently, you have a double
as the first type (the key) and a string
as the second type (the value). 当前,您将
double
作为第一种类型(键),将string
作为第二种类型(值)。 This would allow you to look up a string
value by using a double
value as a key. 这将允许您通过使用
double
值作为键来查找string
值。
But wait. 可是等等。 Your doubles are representing a percentage, while your strings represent a city name.
您的双打代表百分比,而您的字符串则代表城市名称。 You want to look up a percentage, based on the city name.
您想根据城市名称查找百分比。 So the
string
should be the key in your dictionary, not the double. 因此,
string
应该是字典中的键 ,而不是双键。
Dictionary<string, double> dictionary = new Dictionary<string, double>();
This allow you to get a specific double value by providing the right string: 这允许您通过提供正确的字符串来获取特定的double值:
var percentage = dictionary["Gulborgsund"];
Since the lookup will be based on user input (which is usually unreliable), it would probably be best to use the TryGetValue
method: 由于查找将基于用户输入(通常是不可靠的),因此最好使用
TryGetValue
方法:
double percentage;
if (dictionary.TryGetValue(userInput, out percentage))
{
// Use percentage
}
else
{
// Could not retrieve value
}
string userInput = txtKommuneInput.Text;
double procent;
if (dictionary.TryGetValue(userInput, out procent))
txtKommuneresultat.Text = procent.ToString();
How to fish: 如何钓鱼:
Here is a good start for learning about Dictionary in C#: 这是学习C#词典的一个很好的开始:
https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx#Anchor_8 https://msdn.microsoft.com/zh-cn/library/xfhwa508(v=vs.110).aspx#Anchor_8
Also, MSDN have a lot of useful resources there, please, feel free to explore. 此外,请访问MSDN,那里有很多有用的资源。
How to do it (I think this will be specific to your case, I divided into two options, using city name as key or using percentage as key): 怎么做(我认为这将取决于您的情况,我分为两个选项,使用城市名称作为关键字或使用百分比作为关键字):
using System;
using System.Web.SessionState;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
//Part 1: Using percentage as Key
Dictionary<double, string> dictionary = new Dictionary<double, string>();
dictionary.Add(11.44, "Faxe");
//(...)
dictionary.Add(1.29, "Vordingborg");
//Here is Komune
dictionary.Add(5.89, "Komune");
if (dictionary.ContainsKey(5.89)){
Console.WriteLine(String.Format("Found: {0}!", dictionary[5.89]));
} else {
Console.WriteLine("Not Found!");
}
//Part 2: Using the string as Key
Dictionary<string,double> dictionaryStringPercent = new Dictionary<string,double>();
dictionaryStringPercent.Add("Faxe",11.44);
//(...)
dictionaryStringPercent.Add("Vordingborg",1.0);
//Here is Komune
dictionaryStringPercent.Add("Komune",5.89);
if (dictionaryStringPercent.ContainsKey("Komune")){
Console.WriteLine(String.Format("Found: {0}!", dictionaryStringPercent["Komune"]));
} else {
Console.WriteLine("Not Found!");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.