从字典列表中查找值 [英] find value from a dictionary list
问题描述
有一个非常简单的问题。
我的主表单上有一个公开共享的列表。它被称为textboxestime。
这个字典包含文本框名称的值,以及自创建以来添加了多少个刻度。
我有点用英语知道我想要它做什么,但我不能正确说出来。
如果是主要的话.selectedbox.name = main.textboxestime.name
result = cstr(main.textboxestime.value)
但它的名字是不是字典的成员。
我想如果我能在字典列表中看到这个名字,我可以查询一下吗?
我按照以下方式创建字典
首先点亮新的RichTextBox
首先使用
first.Name =first
textboxestime =新词典(Of RichTextBox,Integer)
textboxestime.Add(first,0)
结束使用
然后在每个刻度线上:
Have a really simple question.
I have a list on my main form which is publically shared. It's called textboxestime.
This dictionary holds values for the name of the textbox, and also how many ticks have been added since it was created.
I kind of know in English what I want it to do, but I can't word it correctly.
if main.selectedbox.name = main.textboxestime.name
result = cstr(main.textboxestime.value)
But It is saying name is not a member of dictionary.
I would have thought if I can see the name within the dictionary list, I could query it?
I create the dictionary as per below
Dim first As New RichTextBox
Using first
first.Name = "first"
textboxestime = New Dictionary(Of RichTextBox, Integer)
textboxestime.Add(first, 0)
End Using
Then on each tick:
Dim textBoxes As List(Of RichTextBox)
textBoxes = textboxestime.Keys.ToList()
For Each TextBox In textBoxes
' Increase time for each TextBox
textboxestime(TextBox) = textboxestime(TextBox) + 1
' Check if TB reached 30 secs
If textboxestime(TextBox) = (timeralert) Then
TextBox.BackColor = Color.Red
TextBox.ForeColor = Color.White
End If
Next
If internaltimer = 300 Then
Close()
End If
推荐答案
我认为你的代码和配方都没有意义问题根本没有。差远了。我几乎肯定你做错了什么。
所以,让我正式回答。System.Collections.Generic.Dictionary< KEY,VALUE>
有助于按KEY查找
实例。它为您提供了O(1)的时间复杂度,用于以下操作:VALUE
Dictionary(TKey,TValue).ContainsKey Method(TKey)(System.Collections.Generic) [ ^ ],
< a href =https://msdn.microsoft.com/en-us/library/bb347013%28v=vs.110%29.aspx> Dictionary(TKey,TValue).TryGetValue Method(TKey,TValue)(System。 Collections.Generic) [ ^ ]。
它应该解释你如何在字典中搜索。如果你做其他事情,你根本不需要字典。而你正在做其他事情。 你从来没有真正使用你的字典来做任何有用的事情。(请参阅我对这个问题的评论。)你使用Keys.ToList()
,但是你可能只有RichTextBox
的实例列表。此外,没有必要让这个成员公开分享。 内部访问修饰符可以做到这一点。最好不要提供比实际需要更多的访问权限。我使用计时器似乎非常怀疑。我们只有在你解释目标时才能讨论它。
无论如何你都要在列表中搜索。慢慢地,在O(N)。此外,您对满足特定条件的所有列表元素执行操作。您也可以使用LINQ或此方法执行此操作: List(T)。 ForEach方法(Action(T))(System.Collections.Generic) [ ^ ]。
但是,我认为这没有任何意义。你应该更好地解释所有这些活动的最终目标。
-SA
I see no sense in your code and in formulation of the problem at all. Not even close. I'm almost sure that you do something wrong.
So, let me answer formally.System.Collections.Generic.Dictionary<KEY, VALUE>
helps to findVALUE
byKEY
instance. It gives you time complexity of O(1), for the following operations:
Dictionary(TKey, TValue).ContainsKey Method (TKey) (System.Collections.Generic)[^],
Dictionary(TKey, TValue).TryGetValue Method (TKey, TValue) (System.Collections.Generic)[^].
It should explain you how to search in dictionary. If you do something else, you don't need dictionary at all. And you are doing "something else". You never really use your dictionary for anything useful. (Please see my comments to the question.) You useKeys.ToList()
, but you could have only the list of the instances ofRichTextBox
. Also, there is no need to have this member "publicly shared". The internal access modifiers would do it. It's better not to give more access than it's really required. And it seems highly suspicious to me that you use a timer. We can discuss it only if you explain the goal.
You search in your list anyway. Slowly, at O(N). Moreover, you do your operation for all list elements meeting certain condition. You can also do it using LINQ or this method: List(T).ForEach Method (Action(T)) (System.Collections.Generic)[^].
However, I don't think it makes any sense. You should better explain the ultimate goal of all this activity.
—SA
这篇关于从字典列表中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!