友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
狗狗书籍 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

VB2008从入门到精通(PDF格式英文版)-第111章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!




                  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 takes place。 Instantiating the type  

ArrayList is the instantiation of a collection manager。 The ArrayList instance is then assigned  

to the variable objects; which is of type IList。 IList is an interface making it possible to use the  

collection in the context of a ponent…oriented development environment。 To add two objects  

to the collection; we call the Add() method twice。 To iterate the elements in the collection; we  

use the  For Each statement。 



■Note  The fact that the collection classes can be used in the context of a ponent…oriented application  

is no coincidence。 When Microsoft created its  library; ponents were an essential part of the library。  



     To run the tests; open Module1。vb in your console application and edit it as follows: 



Module Module1 

    Sub Main() 

        BeforeVisualBasic8。Tests。RunAll() 

        Console。ReadKey() 

    End Sub 

End Module 



     Press Ctrl+F5 to run the application and see the results。 



■Note  We did not need to import the System。Collections namespace because it is imported by default  

in Visual Basic 2008。 


…………………………………………………………Page 254……………………………………………………………

232       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  



          The Problem of Mixed Types 



          What is unique about the sample code is that the  For Each statement works and happens to  

          know that the objects in the collection are of type Example。 However; the following code adds a  

          different object to the collection; which will cause the iteration to fail。 



          Class Another  

          End Class 



          Dim objects As IList = New ArrayList() 



          objects。Add(New Example With {。Value = 10}) 

          objects。Add(New Example With {。Value = 20}) 

          objects。Add(New Another()) 

          For Each obj As Example In objects 

                 Console。WriteLine(〃Object value (〃 & obj。Value & 〃)〃) 

返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!