Dictionary Key Change

I am very new to dictionaries. A very new meaning that I started using them about 6 hours ago: p. In any case, I want to know if there is a way to change the dictionary key.

Here is my dictionary:

Dictionary<string, string> Information = new Dictionary<string, string>(); 

This is how I add to the dictionary (it starts every time a user enters information and presses a button:

 Information.Add(txtObjectNumber.Text, addressCombined); 

The user should be able to edit both fields, as well as delete the entire record.

Basically the application should add txtNumber and txtComments , where txtNumber = txtObjectNumber

Thank you for your help.

+6
source share
5 answers

The key is a mechanism that allows you to later find data ("value").

For example, if you did

 information.Add("Kris", "Vandermotten"); 

you can find Vandermotten later if you know Chris.

Now in this context, what does it mean to change Chris? You put the data under the name "Chris" and want to return it in search of "Bob"? You will not find it.

In a sense, dictionary-dictionary is very similar to primary keys in a relational database. Pay attention to the logical identification of the value. Therefore, firstly, they must uniquely identify it.

So perhaps this example does not make sense. Maybe something like

 information.Add(42, new Person("Kris", "Vandermotten") 

makes more sense. Then the question, of course, is: what is 42? Sometimes there is a natural candidate for such a key as an employee number or something else, sometimes not.

When not there, maybe you need to do

 List<Person> information = new List<Person>(); information.Add(new Person("Kris", "Vandermotten")); 

And of course, if the Person object allows you to change the property of the first name and what you want to do, then do it. But "changing dictionary keys" does not make much sense to me.

+3
source

It is not possible to directly modify a key. You will have to remove it and re-add.

+10
source

You can use the built-in Remove () method for your Dictionary. Or you could do it the hard way, iterating through the collection. Although I'm curious why you need to constantly update keys, not just values.

+1
source

You cannot change the key of an existing dictionary. What you can do is delete the previous key and add a new key / value based on editing.

Usage example:

Add Key: Name, Value: Bob

Now you want to change Name to FirstName , you will need to do

Remove the Name key from the dictionary Add the key: FirstName, Value: Bob

0
source

Your use of the dictionary here seems a bit off. Like the name, dictionaries are designed to provide you with a way to "search" for values ​​based on a key. In your case, however, you seem to be using it to store two related pieces of data, each of which can change. In this case, you are probably best off using a list and creating a separate class that encapsulates both parts of the information. Sort of

 public class MyData { public string SomeData { get; set; } public string OtherData { get; set; } } 

and then

 List<MyData> myDataList; 

Or you can even have a dictionary that maps a non-changing key (possibly a user id) to a user class:

 Dictionary<string, MyData> myDataDictionary; 
0
source

Source: https://habr.com/ru/post/918027/


All Articles