Reputation: 41
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
I am new to C#, so I hope someone can help me
I have tried searching for days now, nothing works.I am using Windowsforms, and have the buttonHandler ready
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();
Upvotes: 2
Views: 5916
Reputation: 41
string userInput = txtKommuneInput.Text;
double procent;
if (dictionary.TryGetValue(userInput, out procent))
txtKommuneresultat.Text = procent.ToString();
Upvotes: 0
Reputation: 468
How to fish:
Here is a good start for learning about Dictionary in C#:
https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx#Anchor_8
Also, MSDN have a lot of useful resources there, please, feel free to explore.
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!");
}
}
}
Upvotes: -1
Reputation: 7457
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). This would allow you to look up a string
value by using a double
value as a key.
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.
Dictionary<string, double> dictionary = new Dictionary<string, double>();
This allow you to get a specific double value by providing the right string:
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:
double percentage;
if (dictionary.TryGetValue(userInput, out percentage))
{
// Use percentage
}
else
{
// Could not retrieve value
}
Upvotes: 5