Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stringify key-value pairs in dictionary

Tags:

c#

dictionary

I have made a dictionary which contains two values: a DateTime and a string.

Now I want to print everything from the dictionary to a Textbox. Does anybody know how to do this?

I have used this code to print the dictionary to the console:

private void button1_Click(object sender, EventArgs e) {     Dictionary<DateTime, string> dictionary = new Dictionary<DateTime, string>();     dictionary.Add(monthCalendar1.SelectionStart, textBox1.Text);      foreach (KeyValuePair<DateTime, string> kvp in dictionary)     {         //textBox3.Text += ("Key = {0}, Value = {1}", kvp.Key, kvp.Value);         Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);     } } 
like image 819
Barry The Wizard Avatar asked Feb 17 '15 00:02

Barry The Wizard


People also ask

Does dictionary have key-value pairs?

Since a dictionary has a dynamic structure, you can add new key-value pairs to it at any time.

Can keys in dictionary be string?

The keys of a dictionary can be any kind of immutable type, which includes: strings, numbers, and tuples: mydict = {"hello": "world", 0: "a", 1: "b", "2": "not a number" (1, 2, 3): "a tuple!"}


1 Answers

Just to close this

foreach (KeyValuePair<DateTime, string> kvp in dictionary) {     //textBox3.Text += ("Key = {0}, Value = {1}", kvp.Key, kvp.Value);     Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } 

Changes to this

foreach (KeyValuePair<DateTime, string> kvp in dictionary) {     //textBox3.Text += ("Key = {0}, Value = {1}", kvp.Key, kvp.Value);     textBox3.Text += string.Format("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } 
like image 76
CheGueVerra Avatar answered Sep 21 '22 14:09

CheGueVerra