HashTable ?????? ??? Visual Basic 2005 ?? Visual Basic .NET ??? ??? ??? ???? ?? ??? ???? ????

???? ?????? ???? ??????
???? ID: 307933 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ???? ?? HashTable ?????? ?? ???? ??? ??????? ???? ??? ???? ?? costly ??? ?? ??? ?????? ???? ??????? ???? ?? ??? eliminates hashing, ??????? ?? ????? ?? ???? hashing ?????? ???? ??????? ???? ?? ???? Hashing ?? ???? ?? ????? ?? ??? ????? ????? ?? ??? ?? ????? ???? ???

?? ????? ??????????? ?? ???????? ??HashTable???? ??? ???????? ??System.Collectionsnamespace so that you are not required to code your own hash tables.

????? ????? ???? ?? ??? ???

A HashTable collection stores a(Key, Value)pair and uses the?????to hash and obtain the storage location. The?????is immutable and cannot have duplicate entries in theHashTable. This sample uses several instances of a simplePersonclass to store in aHashTable. The last name is used as the?????.
  1. Open Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and create a Windows Application project in Visual Basic .NET or in Visual Basic 2005. Form1 ???????? ??? ?? ????????? ??? ????? ???
  2. In Solution Explorer, right-click the project name, point toadd?? ????-????? ????, ?? ???? ???????? ??????to add a??????????? ??? Class1 is added to the project by default.
  3. ??????? ???? ??? ?????????? ????? ???, ????? ?????????.

    ???:Visual Studio 2005, ??? ????? ????add?? ?????????.
  4. Replace any code in theClass1module with the following code:
    Public Class Person
        Public Fname, Lname As String
    
        Sub New(ByVal FirstName As String, ByVal LastName As String)
            Fname = FirstName
            Lname = LastName
        End Sub
    
        Public Overrides Function ToString() As String
            Return Fname + " " + Lname
        End Function
    End Class
    						
    ThePersonclass has one constructor that takes theFirstName, ??LastNameparameters and assigns these parameters to the local variables. TheToStringfunction overridesToString??????????class to returnFname, ??Lnameconcatenated together.
  5. In Form1.vb, add the followingImportsstatement to the top of the code module:
    Imports System.Collections
    					
  6. Create a form-levelHashtableobject, and declare three variables of typePerson. Add the following code to the Form1 class:
    Dim MyTable As New Hashtable()
    
    'For simplicity, create three Person objects to add to the HashTable collection.
    
    Dim Person1, Person2, Person3 As Person  
    					
  7. In the following steps, use theadd?? ??? ????Hashtableobject to add threePersonobjects to theHashtable??? ???catch-?? ?????? ????????? ??? Thecatch-?? ?????? ????block catches the exception and displays a message if duplicate keys exist.
    1. Place a???control on Form1, and change the??????? ?? ??? ???Add Elements.
    2. Double-click the button to open its Code window, and paste the following code in theButton1_Click?????:
      Person1 = New Person("Karen", "Berge")
      Person2 = New Person("David", "Campbell")
      Person3 = New Person("Jim", "Kim")
      
      'The Add method takes Key as the first parameter and Value as the second parameter.
      
      Try
           MyTable.Add(Person1.Lname, Person1)
           MyTable.Add(Person2.Lname, Person2)
           MyTable.Add(Person3.Lname, Person3)
      Catch ae As ArgumentException
           MessageBox.Show("Duplicate Key")
      End Try
      						
  8. TheHashtableobject provides an indexer. In the following steps, index with the?????to access the value that is stored at the hashed location.
    1. ????? ??????control to Form1, and change the??????? ?? ??? ???Get Items.
    2. ??? ?? ???-????? ????, ?? ??? ????? ??? ???????Button2_Click?????:
      'Use the indexer of the Hashtable class to retrieve your objects. The indexer takes 
      'Key as a parameter and accesses it with the Hashed location.
              
      Try
           MessageBox.Show(MyTable(Person1.Lname).ToString)
           MessageBox.Show(MyTable(Person2.Lname).ToString)
           MessageBox.Show(MyTable(Person3.Lname).ToString)
      Catch ex As NullReferenceException
           MessageBox.Show("Key not in Hashtable")
      End Try
      						
  9. ??? ????? ????? ?? ??????????HashTable ?????? ??? ?? ???? ?? ???? ?? ????? ?? ??? ????:
    1. ????? ??????Form1 ?? ??? ????????? ????, ?? ????????? ??????????? ?? ??? ??????? ???????.
    2. ??? ?? ???-????? ????, ?? ??? ????? ??? ???????Button3_Click?????:
      'Use the Count property.
      If (MyTable.Count = 0) Then
           MessageBox.Show("There are no items in HashTable")
      Else
           MessageBox.Show("The count before removing an Item is" & " " & MyTable.Count)
           MessageBox.Show("Removing value stored at key value (Berge)")
           'Remove the object that is stored at the Key value Person1.Lname.
           MyTable.Remove(Person1.Lname)
      End If
      						
  10. ????? ????? ??? HashTable ?????? ??? ???????? ?????? ?? ???? ?????
    1. ????? ??????Form1 ?? ??? ????????? ????, ?? ????????? ??????????? ?? ??? ???????.
    2. ??? ?? ???-????? ????, ?? ??? ????? ??? ???????Button4_Click?????:
      Dim Enumerator As IDictionaryEnumerator
      Enumerator = MyTable.GetEnumerator()
      
      If (MyTable.Count = 0) Then
           MessageBox.Show("The HashTable is empty")
      Else
           MessageBox.Show("Enumerating through the HashTable collection")
           While Enumerator.MoveNext()
                MessageBox.Show(Enumerator.Value.ToString())
           End While
      End If
      
      Dim MyKeys As ICollection
      Dim Key As Object
      
      If (MyTable.Count = 0) Then
           MessageBox.Show("The HashTable is empty")
      Else
           MessageBox.Show("Accessing keys property to return keys collection")
           MyKeys = MyTable.Keys()
      
           For Each Key In MyKeys
                MessageBox.Show(Key.ToString)
           Next
      End If 
      							
      ?? ??? declares ?? ?? ??????IDictionaryEnumerator?? ???GetEnumeratorHashTable ?????? ?? ?????? ??? ?? ????????????????? ??? ??, ??? ?????? ??? ?????? ?? ?????? ?? enumerates ?? ?? ????? ???? ???????????? ??? ????HashTable???????? ?? ?????? ?? ????????? ???? ?? ????
  11. ??? ????? ????? ?? ??????????????? ???? ?? ??? ????HashTable.
    1. ????? ??????Form1 ?? ??? ????????? ????, ?? ????????? ??????????? ?? ??? ?????????.
    2. ??? ?? ???-????? ????, ?? ??? ????? ??? ???????Button5_Click?????:
      MyTable.Clear()
      MessageBox.Show("HashTable is now empty")
      						
  12. ????? ?? ????????? ?? ????? ?? ??? ????? ????? ?? ???? ????:
    1. ????? ????,???? ??????. ?? ??? ?????????????????? HashTable ?????? ??? ???? ???? ????
    2. ????? ????,???? ??????? ????. ??????????? obtains HashTable ?????? ??? ?????? ??? ????? ??? ?? ????? ?? ???? ????????? ??? ???? ????
    3. ????? ????,???? ???????. ??? ???? ?? ???? "Berge" ????? ????? ?? ??? ?? ???? ???
    4. ????? ????,????. ?? ???IDictionaryEnumeratorenumerates HashTable ?????? ??? ?????? ?? ?????? ?? ?????
    5. ????? ????,??????. ????? ??? ?? HashTable ?????? ?? ??? ???? ????? ????
???:: ?????? ????????, ?????, ??????, ????? ???, ?-??? ???, ????, ???, ?????, ?? ??????? ??? ???? fictitious ???? ???? ?? ???????? ?????, ?????, ??????, ????? ???, ?-??? ???, ????, ???????, ?????, ?? ????? ?? ??? ??? ????? ???? ??, ?? inferred ???? ??????

???

???? ID: 307933 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
??????: 
kbvs2005swept kbvs2005applies kbhowtomaster kbmt KB307933 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:307933

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com