Lm th? no ? lm vi?c v?i b? su t?p HashTable trong Visual Basic.NET hay Visual Basic 2005

D?ch tiu ? D?ch tiu ?
ID c?a bi: 307933 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t ny cung c?p thng tin v? b? su t?p HashTable. V? bm gip lo?i b? s? c?n thi?t ph?i t?n km t?m ki?m d? li?u truy xu?t d? li?u, b?n c th? dng bm hi?u qu? truy xu?t d? li?u. Bm s? d?ng gi tr? c?a kha ring c?a m?nh ? xc ?nh v? tr c?a d? li?u.

Th vi?n l?p c s? cung c?p m?t HashTable l?p h?c ?c ?nh ngh?a trong cc System.collections khng gian tn do b?n khng ph?i m? c?a ring b?n b?ng bm.

Cc b?c ? xy d?ng cc m?u

M?t b? su t?p HashTable c?a hng m?t (Phm, gi tr?) c?p v s? d?ng cc Key bm v c ?c v? tr lu tr?. Cc Key l khng thay ?i v khng th? c m?c trng l?p trong cc HashTable. M?u ny s? d?ng m?t vi tr?ng h?p c?a m?t n gi?n Ng?i l?p ? lu tr? trong m?t HashTable. Tn cu?i cng ?c s? d?ng nh cc Key.
  1. Microsoft Visual Studio m?.NET ho?c Microsoft Visual Studio 2005, v t?o ra m?t ?ng d?ng Windows d? n trong Visual Basic.NET hay Visual Basic 2005. Form1 s? ?c thm vo cc d? n theo m?c ?nh.
  2. Trong gi?i php Explorer, b?m chu?t ph?i vo tn d? n, i?m ?n Thm, sau b?m Thm Class ? thm m?t L?p h?c m-un. Class1 s? ?c thm vo cc d? n theo m?c ?nh.
  3. Trong cc Thm m?c m?i h?p tho?i h?p, b?m vo M?.

    Chu y Trong Visual Studio 2005, nh?p vo Thm Thay v? M?.
  4. Thay th? b?t k? m? s? trong cc Class1 m-un v?i o?n m? sau:
    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
    						
    Cc Ng?i l?p c m?t trong nh?ng nh xy d?ng s? a cc FirstName v Th? cc tham s? v gn cc tham s? cho cc bi?n ?a phng. Cc ToString ch?c nng ghi ToString t? cc ?i t?ng l?p ? tr? v? Fname v Lname n?i v?i nhau.
  5. Trong Form1.vb, thm d?i y Nh?p kh?u tuyn b? ln ?u cc m-un m?:
    Imports System.Collections
    					
  6. T?o ra m?t h?nh th?c c?p Hashtable ?i t?ng, v tuyn b? ba bi?n lo?i Ng?i. Thm m? sau vo cc l?p h?c Form1:
    Dim MyTable As New Hashtable()
    
    'For simplicity, create three Person objects to add to the HashTable collection.
    
    Dim Person1, Person2, Person3 As Person  
    					
  7. Trong cc b?c sau y, s? d?ng cc Thm phng php c?a cc Hashtable ?i t?ng ? thm ba Ng?i cc ?i t?ng ? cc Hashtable trong m?t c? g?ng n?m b?t kh?i. Cc c? g?ng n?m b?t ch?n b?t ngo?i l? v hi?n th? m?t thng bo n?u trng l?p phm t?n t?i.
    1. Ni m?t Nt i?u khi?n trn Form1 v thay ?i cc Vn b?n b?t ?ng s?n ? Thm cc y?u t?.
    2. B?m p nt ? m? c?a s? m? c?a n, v dn m? sau y trong cc Button1_Click s? ki?n:
      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. Cc Hashtable ?i t?ng cung c?p m?t ch? m?c. Trong cc b?c sau y, ch? m?c v?i cc Key ? truy c?p vo gi tr? ?c lu tr? t?i v? tr bm nh? t?ng.
    1. Thm m?t Nt i?u khi?n ? Form1 v thay ?i cc Vn b?n b?t ?ng s?n ? Nh?n ?c ghi.
    2. B?m p chu?t vo nt, v dn m? sau y trong cc Button2_Click s? ki?n:
      '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. Trong cc b?c sau y, s? d?ng cc G bo phng php ? lo?i b? m?t m?c t? b? su t?p HashTable:
    1. Thm m?t Nt i?u khi?n ? Form1 v thay ?i cc Vn b?n b?t ?ng s?n ? Lo?i b? kho?n m?c.
    2. B?m p chu?t vo nt, v dn m? sau y trong cc Button3_Click s? ki?n:
      '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. Trong cc b?c sau y, li?t k cc m?c ?c ch?a trong b? su t?p HashTable.
    1. Thm m?t Nt i?u khi?n ? Form1 v thay ?i cc Vn b?n b?t ?ng s?n ? K khai.
    2. B?m p chu?t vo nt, v dn m? sau y trong cc Button4_Click s? ki?n:
      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 
      							
      M? ny tuyn b? m?t bi?n lo?i IDictionaryEnumerator v cc cu?c g?i cc GetEnumerator phng php thu th?p HashTable. V?i cc S? ?m tr? v?, cc m? li?t k thng qua cc kho?n m?c trong b? su t?p v s? d?ng cc Phm phng php c?a cc HashTable li?t k thng qua cc phm.
  11. Trong cc b?c sau y, s? d?ng cc R? rng phng php ? xa cc HashTable.
    1. Thm m?t Nt i?u khi?n ? Form1 v thay ?i cc Vn b?n b?t ?ng s?n ? R? rng.
    2. B?m p chu?t vo nt, v dn m? sau y trong cc Button5_Click s? ki?n:
      MyTable.Clear()
      MessageBox.Show("HashTable is now empty")
      						
  12. Th?c hi?n theo cc b?c sau ? xy d?ng v ch?y cc ?ng d?ng:
    1. Nh?p vo Thm cc kho?n m?c. Lu ? r?ng ba Ng?i cc ?i t?ng ?c b? sung vo b? su t?p HashTable.
    2. Nh?p vo Nh?n ?c ghi. Lu ? r?ng cc indexer l?y ?c cc kho?n m?c trong b? su t?p HashTable. Ba m?i ?c thm vo m?c ?c hi?n th?.
    3. Nh?p vo Lo?i b? kho?n m?c. Lu ? r?ng m?c t?i v? tr ch? ch?t "Berge" s? b? xa.
    4. Nh?p vo K khai. Lu ? r?ng IDictionaryEnumerator li?t k thng qua cc kho?n m?c trong b? su t?p HashTable.
    5. Nh?p vo R? rng. Lu ? r?ng t?t c? cc m?c ?c xa t? b? su t?p HashTable.
LU ?: Cc v d? cc cng ty, t? ch?c, s?n ph?m, tn mi?n, ?a ch? e-mail, bi?u trng, ng?i, ?a i?m v cc s? ki?n ?c miu t? ? y l h c?u. Khng c lin k?t v?i b?t k? cng ty th?c s?, t? ch?c, s?n ph?m, tn mi?n, ?a ch? email, bi?u t?ng, ng?i, ?a i?m ho?c s? ki?n ny ?c d? ?nh ho?c nn ?c suy ra.

Thu?c tnh

ID c?a bi: 307933 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
T? kha:
kbvs2005swept kbvs2005applies kbhowtomaster kbmt KB307933 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:307933

Cung cp Phan hi

 

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