Let’s finish the previous part of the lesson in C # about dictionaries today, we’ll do a full dictionary
I promised in the previous lesson that I will describe more of the dictionary functions, there is not much to describe here, because the dictionary has most of the same functions as the list and function of the dictionary, most also work the same as the functions of the list here are some sample functions of dictionaries:
- We have previously used this feature:
Dictionary <string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("some key", "some value");
- Finding values based on the key:
string searchKey = dictionary["some key"];
- Removing the element
As in the lists, you can delete elements, we give only key in the Remove() function, because the value can be the same in the dictionary with two keys, so the compiler will know exactly what to delete.
dictionary.Remove("some key");
- Downloading the list of keys
We can get a list of keys using the Keys property and display them using the foreach loop in the following way:
foreach(string keys in dictionary.Keys){.....}
Dictionaries are really very useful, you can store strings, numbers, objects etc. in them.
Dictionary <string, string> dictionary = new Dictionary<string, string>();
dictionary.Add(231, new Human(){FirstName = Paul, Surname = Kaspersky});
At the end we will write a program that uses the dictionary, just as I promised at the beginning, remove everything from the for what we gave in the first part about dictionaries and make this form:
The drop down list is a combobox control, fields where a value is expected are Textbox, and controls with plain text are label, everything as I wrote earlier is in the Toolbox tab on the left.
Well, now you have to handle it all, so let’s create a ListOfGuests class and put in this code:
class ListOfGuests
{
public string Name { get; set; }
public int Age { get; set; }
public ListOfGuests(string name, int age)
{
Name = name;
Age = age;
}
}
Name and Age properties are the fields in which we holding the data, and below the property we created a constructor in which we will assign data to these fields, let’s go to the form code, but first press twice on the combobox control to create its method and enter in the code form such thing:
namespace Dictionary
{
public partial class Form1 : Form
{
Dictionary<int, ListOfGuests> listofguests = new Dictionary<int, ListOfGuests>()
{
{0, new ListOfGuests("Yodo",34) },
{0, new ListOfGuests("Paul",34) },
{0, new ListOfGuests("Jasmine",34) },
{0, new ListOfGuests("Michael",34) },
{0, new ListOfGuests("Jackson",34) },
{0, new ListOfGuests("Soap",34) },
};
public Form1()
{
InitializeComponent();
foreach(int key in listofguests.Keys)
{
comboBox1.Items.Add(key);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ListOfGuests list = listofguests[(int)comboBox1.SelectedIndex];
Name.Text = list.Name;
Age.Text = list.Age.ToString();
}
}
}
And what is it all about? At the top we have created a dictionary with a list of guests and we inserted there numbers for the keys and created objects of the class ListOfGuests is probably understandable.
Then, at the bottom in the Form1 constructor, the InitializeComponent() function is present, but it is always there and it must be there to initialize all the functions of the form.
Under the InitializeComponent() function, I created a loop in which to extract all the keys from the dictionary and put it in the combobox1 control.
In the function combobox control, which we created earlier under the name comboBox1_SelectedIndexChanged, we create a ListOfGuests class object and insert the currently selected key number from the dictionary but first we replace it with the int type but it will also work without projection, and finally by using the appropriate number we display the dictionary values on the screen.
This content also you can find on my blog http://devman.pl/csharplan/c-language-16-dictionary-part-2/
If you recognise it as useful, share it with others so that others can also use it.
Leave upvote and follow and wait for next articles :) .
And that’s what I think, it is not too difficult, in the next lesson we will say a little about exceptions, see you!
Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
http://devman.pl/en/csharplan/c-language-16-dictionary-part-2/
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit