C# language: 16. Dictionary part 2

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:

1. We have previously used this feature:

2. Finding values based on the key:

3. 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.

4. 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:

Dictionaries are really very useful, you can store strings, numbers, objects etc. in them.

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:

rozwijany formularz z wykorzystaniem słownika

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:

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:

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 steemit blog: https://steemit.com/programming/@slawas/c-language-16-dictionary-part-2

And on medium: https://medium.com/@sawomirkowalski/c-language-16-dictionary-part-2-d44c122be11d

And that’s what I think, it is not too difficult, in the next lesson we will say a little about exceptions, see you!

 
If that post was useful for you share it with your friends :)

Post a comment

Be first!

avatar
  Subscribe  
Notify about