???? ??? ??????? ??????? ??? ?????? HashTable. ??? ??????? ???? ?????? ????? ??????? ???????? ???????? ????????, ????? ??????? ??????? ???????? ???????? ???? ????. ????? ?????? ???? ??????? ???? ?????? ???? ????????.
???? "?????? ??? ?????" ???
HashTable ?? ????? ?? ????? ?????
System.Collections ???? ?? ??? ???????? ??? ??????? ???????.
??????? ??????? ?????? ?? ?????
?????? HashTable ?????? ???
(????? ? ??????) ???????
????? ??????? ??????? ??? ???? ???????.
????? immutable ??? ?? ???? ???? ??????? ????? ??
HashTable. ?????? ??? ??????? ??? ?????? ?????
????? ????? ?????? ??
HashTable. ??? ??????? ??? ??????? ??? ???
?????.
- ???? Microsoft Visual Studio .NET ?? Microsoft Visual Studio 2005 ?? ????? ????? ????? Windows ?? Visual Basic .NET ?? ?? Visual Basic 2005. ??? ????? Form1 ??? ??????? ???? ???????.
- ?? "?????? ??????" ???? ??? ?????? ?????? ??? ??? ??????? ? ??? ??? ????? ? ?? ???? ??? ????? ??? ?????? ??? ???? ?????. ??? ????? Class1 ??? ??????? ???? ???????.
- ?? ???? ?????? ????? ???? ???? ? ???? ??? ???.
?????? ?? Visual Studio 2005 ? ???? ??? ????? ????? ?? ???. - ?????? ??? ??????? ?????? ?? ?????? ??????? Class1 ???????? ???????? ???????:
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
????? ??? ??????? ????? ???? ???? ???? ???????? ????? ????? "?" ??? ??????? ????? ?????? ??? ???????? ??? ????????? ???????. ?????? ?????? ToStringToString ?? ??? ???? ?????? FnameLname ?????? ????. - ?? Form1.vb ? ??? ????? ?????? ????????? ??????? ??? ???? ?????? ??????? ????????? ????????:
Imports System.Collections
- ?? ?????? ???? ???? Hashtable ????? ??????? ?? ??????? ????? ????????? ?? ????? ?????. ?? ?????? ????????? ???????? ??????? ??? ??? Form1:
Dim MyTable As New Hashtable()
'For simplicity, create three Person objects to add to the HashTable collection.
Dim Person1, Person2, Person3 As Person
- ?? ??????? ??????? ??????? ??????? Add???? ???? Hashtable ????? ????? ?????? ????? ??? Hashtable ?? ???? ???????? catch. ???? ???????? catch catches ????????? ???? ????? ?? ???? ???? ?????? ?????.
- ??? ???? ???? ?? ??? Form1 ?????? ??????? Text ??? ????? ?????.
- ???? ????? ??????? ??? ???? ??? ???? ????????? ???????? ?????? ?? ?? ?? ???? ???????? ???????? ??????? ?? ????? Button1_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
- ???? ???? Hashtable ???? ?????. ?? ??????? ???????? ???? ??????? ?????? ??? ?????? ??????? ?? ?????? ???????.
- ????? ???? ???? ?? ??? Form1 ?????? ??????? Text ??? ????? ???????.
- ???? ????? ??????? ??? ???? ?? ?? ???? ???????? ???????? ??????? ?? ????? 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
- ?? ??????? ???????? ?????? ??????? ????? ?????? ???? ???? ?? ?????? HashTable:
- ????? ???? ???? ?? ??? Form1 ?????? ??????? Text ??? ????? ??????.
- ???? ????? ??????? ??? ???? ?? ?? ???? ???????? ???????? ??????? ?? ????? 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
- ?? ??????? ??????? ????? ??????? ???? ??? ??????? ?? ?????? HashTable.
- ????? ???? ???? ?? ??? Form1 ?????? ??????? Text ??? ???????.
- ???? ????? ??????? ??? ???? ?? ?? ???? ???????? ???????? ??????? ?? ????? 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
????????? ???????? ??? ??????? ?? ?????? ??? IDictionaryEnumerator ??? ???????? ??????? GetEnumerator ???????? HashTable. ?? ???? ????? ? ???????? ???????? ????? ???? ??????? ???????? ?? ???????? ??????? ????? ??????HashTable ????? ???? ????????.
- ?? ??????? ???????? ?????? ??????? ??? ???? HashTable.
- ????? ???? ???? ?? ??? Form1 ?????? ??????? Text ??? ???.
- ???? ????? ??????? ??? ???? ?? ?? ???? ???????? ???????? ??????? ?? ????? Button5_Click:
MyTable.Clear()
MessageBox.Show("HashTable is now empty")
- ???? ??????? ??????? ?????? ??????? ???????:
- ???? ??? ????? ???????. ???? ?? ??? ????? ????? ?????? ????? ??? ???????? HashTable.
- ???? ??? ?????? ??? ???????. ???? ?? ??????? ???? ??? ??????? ???????? ?? ?????? HashTable. ????? ??????? ?????? ??? ??? ???????.
- ???? ??? ????? ??????. ???? ??? ??? ??? ?????? ?? ?????? "????" ???????.
- ???? ??? ?????. ???? ????? IDictionaryEnumerator ???? ??????? ???????? ?? ?????? HashTable.
- ???? ??? ???. ???? ?? ??? ??? ???? ??????? ?? ?????? HashTable.
??????: ??????? ? ???????? ? ?????? ? ????? ?????? ? ?????? ?????? ?????????? ? ???????? ? ???????, ? ???????? ?????? ????? ??? ?????? ?????. ?? ????? ?? ???? ? ??????? ? ??????, ??? ?????? ? ????? ?????? ?????????? ? ???? ? ????? ?? ????? ?? ????? ?? ??? ??? ??????? ??? ?????? ?? ??? ?????.
???? ???????: 307933 - ????? ??? ??????: 15/?? ??????/1427 - ??????: 3.3
????? ???
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
| kbmt kbvs2005swept kbvs2005applies kbhowtomaster KB307933 KbMtar |
????? ???????: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????
307933
(http://support.microsoft.com/kb/307933/en-us/
)