Lm th? no ? lm vi?c v?i b? su t?p HashTable trong Visual C#

D?ch tiu ? D?ch tiu ?
ID c?a bi: 309357 - Xem s?n ph?m m bi ny p d?ng vo.
M?t phin b?n Microsoft Visual Basic.net c?a bi vi?t ny, xin xem 307933.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

Tm t?t

Bi vi?t t?ng b?c ny cho b?n th?y lm th? no ? s? d?ng b? su t?p HashTable. B?i v? bm gip lo?i b? s? c?n thi?t cho vi?c tra c?u t?n t?m nh?n th?p c?a d? li?u ? truy xu?t d? li?u, b?n c th? s? d?ng bm ? hi?u qu? l?y d? li?u. Bm s? d?ng gi tr? c?a chnh b?n thn ? xc ?nh v? tr c?a d? li?u.

Th vi?n l?p c s? cung c?p m?t l?p HashTable ?c ?nh ngh?a trong khng gian tn System.Collections do b?n khng ?c yu c?u ? m? b?ng bm c?a ring b?n.

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

M?t b? su t?p HashTable mua s?m m?t c?p (Key, gi tr?) v s? d?ng phm ? bm v c ?c v? tr lu tr?. Phm l khng thay ?i v khng th? c m?c trng l?p trong HashTable. M?u ny s? d?ng m?t s? tr?ng h?p c?a m?t t?ng l?p ng?i n gi?n ? lu tr? trong m?t HashTable. Tn cu?i cng ?c s? d?ng l ch?a kha.
  1. M? Microsoft Visual Studio v t?o ra m?t d? n c?a s? h?nh th?c ?ng d?ng trong Visual C#. theo m?c ?nh, Form1 ?c thm vo d? n.
  2. Trong gi?i php Explorer, b?m chu?t ph?i vo tn c?a d? n, i?m ? Thm, v sau nh?p vo l?p ? thm m?t l?p m-un. Class1 ?c thm vo cc d? n theo m?c ?nh.
  3. Thay th? b?t k? m? trong cc m-un Class1 v?i o?n m? sau:
     public class Person
    {
    	public string Fname, Lname;
    
    	public Person(string FirstName, string LastName)
    	{
    		Fname = FirstName;
    		Lname = LastName;
    	}
    
    	public override string ToString()
    	{
    		return Fname + " " + Lname;
    	}
    }
    						
    Cc l?p h?c ng?i ? xy d?ng m?t m m?t cc tham s? FirstName v LastName v ch? ?nh cc tham s? cho cc bi?n ?a phng. Ch?c nng ToString s? thay th? ToString t? cc ?i t?ng l?p ? tr? v? Fname v Lname n?i v?i nhau.
  4. 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 Form1 l?p:
    <?xm-deletion_mark author="v-bobbid" time="20080711T172143-0800" data="private Hashtable MyTable = new Hashtable();
    		
    //For simplicity, create three Person objects to add to the HashTable collection.
    
    Person Person1,Person2,Person3; "?><?xm-insertion_mark_start author="v-bobbid" time="20080711T172143-0800"?>System.Collections.Hashtable MyTable = new 
    System.Collections.Hashtable();
    
    //For simplicity, create three Person objects to add to the HashTable collection.
    
    Person Person1,Person2,Person3; 
    <?xm-insertion_mark_end?>
    					
  5. Trong b?c sau, s? d?ng phng php Thm ?i t?ng Hashtable ? thm ba ng?i ?i t?ng ? Hashtable trong m?t kh?i th?-b?t . Cc kh?i c? g?ng n?m b?t b?t ngo?i l? v hi?n th? m?t thng bo n?u trng l?p phm t?n t?i:
    1. ?t m?t nt ch?n m?t i?u khi?n vo Form1, v thay ?i nh vn b?n ? Thm m?c.
    2. B?m p nt ch?n m?t ? m? c?a s? m? c?a n, v dn o?n m? sau trong tr?ng h?p Button1_Click :
      Person1 = new Person("David", "Burris");
      Person2 = new Person("Johnny", "Carrol");
      Person3 = new Person("Ji", "Jihuang");
      
      //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 (ArgumentException ae)
      {
      	MessageBox.Show("Duplicate Key");
      	MessageBox.Show(ae.Message);
      }
      						
  6. ?i t?ng Hashtable cung c?p m?t m?c ch? d?n. Trong b?c sau, ch? s? v?i phm ? truy c?p vo gi tr? ?c lu tr? t?i bm nh? t?ng v? tr:
    1. Thm m?t nt ch?n m?t ki?m sot ? Form1, v thay ?i ti s?n tn ? Nh?n ?c ghi.
    2. B?m p nt ch?n m?t, v dn o?n m? sau trong tr?ng h?p 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 (NullReferenceException ex)
      {
      	MessageBox.Show("Key not in Hashtable");
      	MessageBox.Show(ex.Message);
      } 
      						
  7. Trong b?c sau, s? d?ng phng php lo?i b? ? lo?i b? m?t m?c duy nh?t t? b? su t?p HashTable:
    1. Thm m?t nt ch?n m?t ki?m sot ? Form1, v thay ?i nh vn b?n ? Lo?i b? kho?n m?c.
    2. B?m p nt ch?n m?t, v dn o?n m? sau trong tr?ng h?p Button3_Click :
      <?xm-deletion_mark author="v-bobbid" time="20080711T173011-0800" data="if (MyTable.Count == 0) 
      {
      	MessageBox.Show(&quot;There are no items in HashTable&quot;);
      }
      else
      {
              MessageBox.Show(&quot;The count before removing an Item is&quot; + &quot; &quot; + MyTable.Count);
              MessageBox.Show(&quot;Removing value stored at key value (Burris)&quot;);
              Remove the object that is stored at the Key value Person1.Lname.
              MyTable.Remove(Person1.Lname);
      }"?><?xm-insertion_mark_start author="v-bobbid" time="20080711T173011-0800"?>if (MyTable.Count == 0) 
      {
          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 (Burris)");
              // Remove the object that is stored at the Key value Person1.Lname.
              MyTable.Remove(Person1.Lname);
      } 
      <?xm-insertion_mark_end?>
      						
  8. Trong b?c sau, li?t k cc m?c ?c lu tr? trong b? su t?p HashTable:
    1. Thm m?t nt ch?n m?t ki?m sot ? Form1, v thay ?i nh vn b?n ? Li?t k.
    2. B?m p nt ch?n m?t, v dn o?n m? sau trong tr?ng h?p Button4_Click :
      <?xm-deletion_mark author="v-bobbid" time="20080711T174252-0800" data="IDictionaryEnumerator Enumerator;
      
      if (MyTable.Count == 0)
      	MessageBox.Show(&quot;The hashtable is empty&quot;);
      else
      {
      	MessageBox.Show(&quot;Enumerating through the Hashtable collection&quot;);
      	Enumerator = MyTable.GetEnumerator();
              
              while (Enumerator.MoveNext())
      	{
      	        MessageBox.Show(Enumerator.Value.ToString());
      	}
      }
      
      ICollection MyKeys;
           
      
      if (MyTable.Count == 0) 
             MessageBox.Show(&quot;The hashtable is empty&quot;);
      else
      {
             MessageBox.Show(&quot;Accessing keys property to return keys collection&quot;);
             MyKeys = MyTable.Keys;
      
             foreach (object Key in MyKeys)
             {
      		MessageBox.Show(Key.ToString());
             }								   
      } 
      							"?><?xm-insertion_mark_start author="v-bobbid" time="20080711T174252-0800"?>System.Collections.IDictionaryEnumerator Enumerator;
      
      if (MyTable.Count == 0)
          MessageBox.Show("The hashtable is empty");
      else
      {
          MessageBox.Show("Enumerating through the Hashtable collection");
          Enumerator = MyTable.GetEnumerator();
      
          while (Enumerator.MoveNext())
          {
              MessageBox.Show(Enumerator.Value.ToString());
          }
      }
      
      System.Collections.ICollection MyKeys;
      
      
      if (MyTable.Count == 0)
          MessageBox.Show("The hashtable is empty");
      else
      {
          MessageBox.Show("Accessing keys property to return keys collection");
          MyKeys = MyTable.Keys;
      
          foreach (object Key in MyKeys)
          {
              MessageBox.Show(Key.ToString());
          }
      }  
      <?xm-insertion_mark_end?>
      M? ny tuyn b? m?t bi?n lo?i IDictionaryEnumerator v g?i phng php GetEnumerator c?a b? su t?p HashTable. V?i s? ?m tr? v?, cc m? li?t k thng qua cc m?c trong b? su t?p v s? d?ng phng php phmHashTable li?t k thng qua cc phm.
  9. Trong b?c sau, s? d?ng phng php xa ? xa HashTable:
    1. Thm m?t nt ch?n m?t ki?m sot ? Form1, v thay ?i nh vn b?n ? R? rng.
    2. B?m p nt ch?n m?t, v dn o?n m? sau trong tr?ng h?p Button5_Click :
      MyTable.Clear();
            MessageBox.Show("HashTable is now empty");
      						
  10. Th?c hi?n theo cc b?c sau ? xy d?ng b?t ln v ch?y cc ?ng d?ng:
    1. B?m vo Thm b?n ghi. Lu ? r?ng ba ng?i ?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 c ?c cc m?c trong b? su t?p HashTable. Ba m?i ?c thm vo m?c ?c hi?n th?.
    3. Nh?p vo xo m?c. Lu ? r?ng m?c t?i i?m quan tr?ng "Burris" s? b? xa.
    4. Nh?p vo li?t k. Lu ? r?ng IDictionaryEnumerator li?t k thng qua cc m?c trong b? su t?p HashTable, v ti s?n phm c?a HashTable tr? v? m?t t?p h?p phm.
    5. Nh?p vo r? rng. Lu ? r?ng t?t c? cc m?c ?c xa t? cc b? su t?p HashTable.
Lu ? V d? 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 s? ki?n ?c m 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, ia chi email, bi?u trng, ng?i, ?a i?m ho?c s? ki?n ?c thi?t k? ho?c suy di?n m?i.

Thu?c tnh

ID c?a bi: 309357 - L?n xem xt sau cng: 03 Thang Ba 2014 - Xem xt l?i: 3.0
p d?ng
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
T? kha:
kbhowtomaster kbmt KB309357 KbMtvi
My d?ch
QUAN TRONG: Bai vit nay c d?ch b?ng phn mm dich thu?t cua Microsoft va co th c Cng ng Microsoft chinh sa l?i thng qua cng ngh CTF thay vi mt bin dich vin chuyn nghip. Microsoft cung c?p cc bi vi?t ?c c? bin d?ch vin v ph?n m?m d?ch thu?t th?c hi?n v c?ng ?ng ch?nh s?a l?i ? 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 nhi?u ngn ng? Tuy nhin, bi vi?t do my d?ch hoc thm chi cng ng chinh sa sau khng ph?i lc no c?ng hon h?o. Cc bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, 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.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny: 309357

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