嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈�《超噌�殻窟�嵌虜隆輓麈觚翹瀘卉韮�仍仍�。� 烏御危列
浪慕利 卦指云慕朕村 厘議慕尺 厘議慕禰 TXT畠云和墮 序秘慕杏 紗秘慕禰

VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及83嫗

梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��






           Some Things for You to Do 



           The following are some exercises that will help you apply the concepts you learned in this chapter。 



                1。  The LightingController。AddRoomGrouping�┌� method has a mistake。 Write some tests to  

                    find the error�察�and then fix the code and rerun your tests to verify that the error has  

                    been fixed。 



                2。  The TestInsert�┌� test method is one example of an insertion test�察�but not all variations  

                    have been tested。 Write another test method that implements the remaining variation��s��  

                    that need to be tested。 



                3。  The declarations of RoomGrouping and Room are not optimal。 Fix the declarations。 



                4。  Implement a general collection class based on the experience of using the class in the  

                    class LightingController。 Hint�此�look at how the linked list for Room is declared and figure  

                    out a way to abstract that into some general collection class。 



                5。  When the method  LightingController。AddRoom�┌� is called�察�the method is tested internally  

                    to see if the handle is of type RoomGrouping。 Can you think of a more defensive pro

                    gramming technique to make sure that the code someone else passes to the kernel will  

                    not cause the kernel to fail�拭�Hint�此�think about the methods to turn on or off the lights  

                    and think of what could go wrong。 


´´´´´´´´´´´´´´´´´´´´´´Page 251´´´´´´´´´´´´´´´´´´´´´´´

C  H  A  P  T  E  R     9 



* * * 



Learning About Lists�察�Delegates�察 �

and Lambda Expressions  



One of the most mon pieces of code that you will write is code that manages many object  

instances。 In the previous examples�察�many object instances were managed using an array。 In  

Chapter 8�察�you learned that a linked list used in conjunction with a default property could make a  

plain´vanilla object look like a collection。 This chapter introduces the  collection classes�察 �

which provide an easy way to manage a set of object instances。 Think of a collection object as  

an infinite sack where things can be added�察�iterated through�察�and retrieved。  

     The chapter begins with a discussion of how to manage collections。 Then we will look at  

different ways of iterating the data using a delegate。 Finally�察�we will look at lambda expressions  

and closures to illustrate another way of processing data。 

     The project structure used in this chapter is a single console application。 As we will not be  

building an overall application�察�but rather a set of sample code snippets�察�no tests or libraries  

are involved。 



Managing Collections 



When you have a collection�察�what you actually have is an object that happens to point to many  

other objects。  

     Visual Basic provides collection classes for managing collections。 Visual Basic 2005 intro

duced a different approach to collections�察�which solved many of the problems that came up  

in earlier Visual Basic versions。 Here�察�we¨ll look at managing collections both before and after  

Visual Basic 2005�察�which will help you to understand how collections are used。  



Managing a Collection Before Visual Basic 2005 



Before Visual Basic 2005�察�the main collection classes were stored in the namespace System。 

Collections ��and�察�of course�察�these classes are still available for you to use��。 The following are  

some of the classes and interfaces in that namespace�此�



                                                                                                          229 


´´´´´´´´´´´´´´´´´´´´´´Page 252´´´´´´´´´´´´´´´´´´´´´´´

230       CH AP T E R   9   *    L E A R N IN G   AB OU T   L I ST S�察  �D E L E G A T E S�察  �A N D   L A M B DA   E X P R E S SI ON S  



               o  ArrayList�此�A general collection that manages all of the referenced objects using an  

                  internal array。 This class manages the problem of increasing the size of an array。 



               o  Hashtable�此�A collection class where the individual objects are stored using key/value  

                  pairs。 In the previous chapter�察�the indexer was used to retrieve a room grouping based  

                  on its identifier。 You could use a Hashtable to acplish the same thing。 



               o  ICollection�此�An interface implement by ArrayList that provides basic functionality that  

                  copies the references to another array。 



               o  IDictionary�此�An interface implemented by Hashtable that allows a programmer to asso

                  ciate a key with a value。 



               o  IList�此�An interface implemented by ArrayList that provides a general´access mechanism  

                  for manipulating a collection of items。 



               o  Queue�此�A collection that implements the first in�察�first out ��FIFO�� mechanism。 You could  

                  use a queue when you are processing a set of instructions。 The first instruction to process  

                  would be the first instruction added to the collection。  



               o  Stack�此�A collection that implements the last in�察�first out ��LIFO�� mechanism。 Think of it as  

                  a stack of papers。 When one piece of paper is laid on top of another�察�the first piece of  

                  paper that is processed is the last piece of paper added to the stack of papers。 



                All of the collection types!ArrayList�察�Hashtable�察�Queue�察�and Stack!implement a way to  

           store a set of types。 The difference in the collection types lies in how the individual objects are  

           stored and retrieved from the collection。 For examples of using these collection types�察�see the  

           ^Learning More About Collection Types ̄ section later in this chapter。 

                Let¨s walk through an example of using these collection classes。 Begin by creating a console  

           application and call it OneToManySamples。 Then add a new class ��right´click your console appli

           cation project and select Add  Class��。 Call it  Example。vb and add all of the following code to it�此�



           Class Example 

              Public Property Value�┌� As Integer 

                  Get 

                      Return _value 

                  End Get 

                  Set��ByVal value As Integer�� 

                      _value = value 

                  End Set 

              End Property 



              Private _value As Integer 

           End Class 



           Friend Module Tests 

              Private Sub PlainVanillaObjects�┌�  

                      Dim objects As IList = New ArrayList�┌� 


´´´´´´´´´´´´´´´´´´´´´´Page 253´´´´´´´´´´´´´´´´´´´´´´´

                  C HA P TE R   9   *    L E AR N I N G   A B O U T  L I ST S�察  �DE L E G AT E S �察  �AN D  L A M B D A  E X PR E SSI O N S  231 



            objects。Add��New Example With ��。Value = 10���� 

            objects。Add��New Example With ��。Value = 20���� 

            For Each obj As Example In objects 

                Console。WriteLine�─�Object value �──�& obj。Value & ;��;�� 

            Next 

    End Sub 

    Public Sub RunAll�┌�  

        PlainVanillaObjects�┌� 

    End Sub 

End Module 



     This is the type of code written before Visual Basic 2005�察�and it follows a standard set of steps�此�



     1。  You define a custom type �─�Example in this example��。 



     2。  You instantiate the custom type and add the instances to a collection。 In the example�察 �

         two instances of Example are added to the collection type ArrayList。  



     3。  The collection is manipulated to allow you to access and manipulate the instances of  

         the custom types。 In the example�察�the collection ArrayList is an interface instance of IList。 



     The bolded code in the example is where the action
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��0�� 家��0��
隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��