简体   繁体   English

更改字典键

[英]Changing the Key of a Dictionary

I am very new to dictionaries. 我是字典的新手。 Very new meaning that I started using them about 6 hours ago :p. 非常新的含义,我大约在6个小时前开始使用它们:p。 Anyways, I want to know if there is a way to change the key of a dictionary. 无论如何,我想知道是否有办法更改字典的键。

Here is my dictionary: 这是我的字典:

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

Here is how I am adding to the dictionary (this is fired every time the user enters info and hits a button: 这是我添加到字典中的方式(每次用户输入信息并单击按钮时都会触发该字典:

Information.Add(txtObjectNumber.Text, addressCombined);

The user needs to be able to edit both fields as well as remove the whole record. 用户需要能够编辑两个字段以及删除整个记录。

So pretty much the application needs to add txtNumber and txtComments where txtNumber = txtObjectNumber 因此,应用程序几乎需要添加txtNumbertxtComments ,其中txtNumber = txtObjectNumber

Thank you for your help. 谢谢您的帮助。

It is not possible to directly modify a key. 无法直接修改密钥。 You'd have to remove it and re-add it. 您必须将其删除并重新添加。

The key is the mechanism that will allow you to find the data (the "value") later. 关键是使您以后可以找到数据(“值”)的机制。

For example, if you did 例如,如果您做了

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

you'd be able to find "Vandermotten" back later if you know "Kris". 如果您知道“ Kris”,便可以稍后找到“ Vandermotten”。

Now in that context, what does it mean to change "Kris"? 现在在这种情况下, 更改 “克里斯”意味着什么? You put data in under the name "Kris" and want to get it back out searching for "Bob"? 您将数据放入“ Kris”名称下,并想找回“ Bob”将其取回来吗? You won't find it. 你找不到。

In a way, dictionary key's are very much like primary keys in a relational database. 从某种意义上说,字典键非常类似于关系数据库中的主键。 The refer to the logical identity of the value. 引用值的逻辑标识。 So for one thing, they should be uniquely identifying it. 因此,一方面,他们应该唯一地标识它。

So maybe this example doesn't make sense. 所以也许这个例子没有意义。 Maybe something like 也许像

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

makes more sense. 更有意义。 The question then of course is: what's the 42? 那么,问题当然是:42是多少? Sometimes there is a natural candidate for such a key, like an employee number or something, sometimes there isn't. 有时有这样一个键的自然候选人,例如员工编号之类的东西,有时没有。

When there is none, maybe you need to do 如果没有,也许您需要做

List<Person> information = new List<Person>();

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

And of course, if a Person object allows changing the first name property, and that's what you want to do, then do it. 当然,如果Person对象允许更改名属性,而这正是您要执行的操作,请执行此操作。 But "changing dictionary keys" doesn't make a lot of sense to me. 但是“更改字典键”对我来说没有多大意义。

You could use the built-in Remove() method for your Dictionary. 您可以为字典使用内置的Remove()方法。 Or you could do it the hard way by iterating through the collection. 或者,您可以通过遍历集合来用困难的方式做到这一点。 Although I'm curious as to why you would need to have to constantly update the keys, and not the values only. 尽管我很好奇为什么您需要不断更新键,而不仅仅是值。

You can't change the key of an existing dictionnary item. 您不能更改现有词典项的键。 What you could do is delete the previous key and add a new key/value based on the edit. 您可以做的就是删除先前的键,然后根据编辑添加新的键/值。

Example use case: 用例示例:

Add Key:Name, Value: Bob 添加键:名称,值:Bob

Now you want to change Name to FirstName , you would have to do 现在,您想将Name更改为FirstName ,您必须做

Remove key Name from dictionnary Add Key:FirstName, Value: Bob 从字典中删除键Name添加键:名字,值:鲍勃

Your use of a dictionary here seems a bit off. 您在这里使用字典似乎有些偏离。 Like the name suggests, dictionaries are intended to provide you a way to "look up" a value, based on the key. 顾名思义,字典旨在为您提供一种基于键来“查找”值的方法。 In your case, though, you seem to be using it to store two related pieces of data, either of which can change. 但是,就您而言,您似乎正在使用它来存储两个相关的数据,其中任何一个都可以更改。 In that case, you're probably better off just using a list, and creating a separate class that encapsulates both pieces of information. 在这种情况下,最好只使用一个列表,然后创建一个单独的类来封装这两部分信息。 Something like 就像是

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

and then 然后

List<MyData> myDataList;

Or, you might even want to have a dictionary that maps a non-changing key (maybe a user id) to the custom class: 或者,您甚至可能希望拥有一个将不变的键(可能是用户ID)映射到自定义类的字典:

Dictionary<string, MyData> myDataDictionary;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM