VB.Net Dictionary的技巧和8个实例
1. Adding keys
First, nearly every program that uses
Dictionary
in VB.NET will require the Add subroutine. The Add subroutine requires two arguments in its parameter list, the first being the key of the element to add, and the second being the value that key should have. Internally, the Add subroutine computes the key's hash code value, and then stores the data in the hash bucket. Because of this step, adding to Dictionary collections is slower than adding to other collections like List.
~~~ Program that uses Dictionary Of String (VB.NET) ~~~
Module Module1
Sub Main()
Dim dictionary As New Dictionary(Of String, Integer)
dictionary.Add("Dot", 20)
dictionary.Add("Net", 1)
dictionary.Add("Perls", 10)
dictionary.Add("Visual", -1)
End Sub
End Module
~~~ Results of program ~~~
The Dictionary has four keys and values.
Using Add subroutine.
The program above will succeed without an exception. However, if you add keys to the Dictionary and one of the keys is already in the Dictionary, you will get an exception. If it is possible the key already exists, always check with ContainsKey that the key is not present. Alternatively, you can catch possible exceptions with Try and Catch.
2. Using ContainsKey
Here we look at the
ContainsKey
function on the Dictionary generic collection in the VB.NET language. The ContainsKey function returns a Boolean value, which means you can use it in an If conditional statement. One common use of ContainsKey is to prevent exceptions before calling Add. Another use is simply to see if the key exists in the hash table, before you take further action.
--- Program that uses ContainsKey (VB.NET) ---
Module Module1
Sub Main()
' Declare new Dictionary with String keys.
Dim dictionary As New Dictionary(Of String, Integer)
' Add two keys.
dictionary.Add("carrot", 7)
dictionary.Add("perl", 15)
' See if this key exists.
If dictionary.ContainsKey("carrot") Then
' Write value of the key.
Dim num As Integer = dictionary.Item("carrot")
Console.WriteLine(num)
End If
' See if this key also exists (it doesn't).
If dictionary.ContainsKey("python") Then
Console.WriteLine(False)
End If
End Sub
End Module
--- Results of program ---
Using ContainsKey Function. This function in the base class library on the Dictionary type returns a Boolean value. If the key was found in the Dictionary, it returns True; if the key was not found, it returns False. You can also store the result of ContainsKey in a Dim Boolean, and test that variable with the = and <> binary operators.
Using Item Function. The Item member is actually a Property accessor, which allows you to set or get an element in the Dictionary. It is equivalent to Add when you assign it. You can assign it to a nonexistent key, but you cannot access a nonexistent key with Item without an exception. [TODO - Add to other article]
3. Looping over keys and values
Here we look at how you can loop over the entries in your Dictionary using the VB.NET language. It is usually easiest to use the For Each iterative statement to loop over the KeyValuePair structures in the Dictionary. In the body of the For Each loop, you can access the KeyValuePair's Key and Value get accessors. They have the types of the Dictionary keys and values so no casting is required.
=== Program that loops over entries (VB.NET) ===
Module Module1
Sub Main()
' Put four keys into a Dictionary Of String.
Dim dictionary As New Dictionary(Of String, Integer)
dictionary.Add("please", 32)
dictionary.Add("help", 16)
dictionary.Add("program", 256)
dictionary.Add("computers", -1)
' Loop over entries.
Dim pair As KeyValuePair(Of String, Integer)
For Each pair In dictionary
' Display Key and Value.
Console.WriteLine("{0}, {1}", pair.Key, pair.Value)
End Sub
End Module
=== Output of the program ===
please, 32
help, 16
program, 256
computers, -1
Understanding KeyValuePair. In the base class library, the KeyValuePair type is implemented as a struct, which means it is used as a value and its contents are contains in the variable storage location itself. This can improve performance in some cases and cause slowdowns in other cases.
[1] [2] [3] 下一页