????? ????? ?? ?????? HashTable ?? "Visual C" #

?????? ????????? ?????? ?????????
???? ???????: 309357 - ??? ???????? ???? ????? ????? ??? ???????.
?????? ??? ????? Microsoft Visual Basic .NET ?? ??? ???????? ???? 307933.
????? ???? | ?? ????

?? ??? ??????

??????

???? ??? ??????? ???? ????? ????? ??????? ?????? HashTable. ??? ??????? ???? ?????? ????? ??????? ???????? ???????? ????????, ????? ??????? ??????? ???????? ???????? ???? ????. ????? ?????? ???? ??????? ???? ?????? ???? ????????.

???? "?????? ??? ?????" ??? HashTable ?? ????? ?? ????? ????? System.Collections ???? ?? ??? ???????? ??? ??????? ???????.

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

?????? HashTable ?????? ??? (????? ? ??????) ??????? ????? ??????? ??????? ??? ???? ???????. ????? immutable ??? ?? ???? ???? ??????? ????? ?? HashTable. ?????? ??? ??????? ??? ?????? ????? ????? ????? ?????? ?? HashTable. ??? ??????? ??? ??????? ??? ??? ?????.
  1. ???? Microsoft Visual Studio ?? ????? ????? ????? ????? Windows ?? "Visual C" #. ??? ????? Form1 ??? ??????? ???? ???????.
  2. ?? "?????? ??????" ???? ??? ?????? ?????? ??? ??? ??????? ? ??? ??? ????? ? ?? ???? ??? ??? ?????? ??? ???? ?????. ??? ????? Class1 ??? ??????? ???? ???????.
  3. ?????? ??? ??????? ?????? ?? ?????? ??????? Class1 ???????? ???????? ???????:
     public class Person
    {
    	public string Fname, Lname;
    
    	public Person(string FirstName, string LastName)
    	{
    		Fname = FirstName;
    		Lname = LastName;
    	}
    
    	public override string ToString()
    	{
    		return Fname + " " + Lname;
    	}
    }
    						
    ????? ??? ??????? ????? ???? ???? ???? ???????? ????? ????? "?" ??? ??????? ????? ?????? ??? ???????? ??? ????????? ???????. ?????? ?????? ToStringToString ?? ??? ???? ?????? FnameLname ?????? ????.
  4. ?? ?????? ???? ???? Hashtable ????? ??????? ?? ??????? ????? ????????? ?? ????? ?????. ?? ?????? ????????? ???????? ??????? ??? ??? Form1:
    <?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. ?? ??????? ??????? ??????? ??????? Add???? ???? Hashtable ????? ????? ?????? ????? ??? Hashtable ?? ???? ???????? catch. ???? ???????? catch catches ????????? ????? ????? ?? ???? ???? ?????? ?????:
    1. ??? ???? ???? ?? ??? Form1 ?????? ??????? Text ??? ????? ?????.
    2. ???? ????? ??????? ??? ???? ??? ???? ????????? ???????? ?????? ?? ?? ?? ???? ???????? ???????? ??????? ?? ????? 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. ???? ???? Hashtable ???? ?????. ?? ??????? ???????? ???? ????? ?????? ?????? ??????? ?? ?????? ???????:
    1. ????? ???? ???? ?? ??? Form1 ?????? ??????? ??? ??? ????? ???????.
    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 (NullReferenceException ex)
      {
      	MessageBox.Show("Key not in Hashtable");
      	MessageBox.Show(ex.Message);
      } 
      						
  7. ?? ??????? ???????? ?????? ??????? ????? ?????? ???? ???? ?? ?????? HashTable:
    1. ????? ???? ???? ?? ??? Form1 ?????? ??????? Text ??? ????? ??????.
    2. ???? ????? ??????? ??? ???? ?? ?? ???? ???????? ???????? ??????? ?? ????? 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. ?? ??????? ??????? ????? ??????? ???? ??? ??????? ?? ?????? HashTable:
    1. ????? ???? ???? ?? ??? Form1 ?????? ??????? Text ??? ???????.
    2. ???? ????? ??????? ??? ???? ?? ?? ???? ???????? ???????? ??????? ?? ????? 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?>
      ????????? ???????? ??? ??????? ?? ?????? ??? IDictionaryEnumerator ??? ???????? ??????? GetEnumerator ???????? HashTable. ?? ???? ????? ? ???????? ???????? ????? ???? ??????? ???????? ?? ???????? ??????? ????? ??????HashTable ????? ???? ????????.
  9. ?? ??????? ???????? ?????? ??????? ??? ???? HashTable:
    1. ????? ???? ???? ?? ??? Form1 ?????? ??????? Text ??? ???.
    2. ???? ????? ??????? ??? ???? ?? ?? ???? ???????? ???????? ??????? ?? ????? Button5_Click:
      MyTable.Clear();
            MessageBox.Show("HashTable is now empty");
      						
  10. ???? ??????? ??????? ?????? ??????? ???????:
    1. ???? ??? ????? ???????. ???? ?? ??? ????? ????? ?????? ????? ??? ???????? HashTable.
    2. ???? ??? ?????? ??? ???????. ???? ?? ??????? ???? ??? ??????? ???????? ?? ?????? HashTable. ????? ??????? ?????? ??? ??? ???????.
    3. ???? ??? ????? ??????. ???? ??? ??? ??? ?????? ?? ?????? "Burris" ???????.
    4. ???? ??? ?????. ???? ?? ????? IDictionaryEnumerator ???? ??????? ???????? ?? ?????? HashTable ????? ???????? HashTable ????? "?????? ????????".
    5. ???? ??? ???. ???? ?? ??? ??? ???? ??????? ?? ?????? HashTable.
?????? ??????? ? ???????? ? ?????? ? ????? ??????, ?????? ?????? ?????????? ? ???????? ? ???????, ? ???????? ?????? ????? ??? ?????? ?????. ?? ????? ?? ???? ? ??????? ? ??????, ??? ?????? ? ????? ?????? ?????????? ? ???? ? ????? ?? ????? ?? ????? ?? ??? ??? ??????? ??? ?????? ?? ??? ?????.

???????

???? ???????: 309357 - ????? ??? ??????: 12/???/1429 - ??????: 4.0
????? ???
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
????? ??????: 
kbmt kbhowtomaster KB309357 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????309357

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

 

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