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

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

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




    End Sub 

End Structure 



■Note  Nothing is a special value that you can assign to reference variables。 It means that the reference  

variable does not point to a real object。 Nothing is a useful value to assign to reference variables in a constructor;  

if you don’t have a meaningful object that they can point to at the outset。 



     To define a constructor; you define a method of type Sub called New()。 And; in most cases;  

you will use public scope。 The parameters of the constructor represent the three pieces of  

information that are required to instantiate a valid state。 Within the constructor; the data  

members are assigned the values of the parameters。 

     The defined constructor has parameters; which means that to instantiate Node; you need  

to provide the three pieces of data。 Thus; to instantiate Node; you need to provide enough data  

to make the node logical。 The original instantiation code would not pile; so to pile the  

code; you need to modify the instantiation to the following: 



Dim city as Node = New Node(〃Montreal〃; 0。0; 0。0) 



     The declaration of the node might reference incorrect data; but that is not the responsibility  

of the intelligent data structure。 An analogy is that a word processor by itself is not responsible for  

making sure that the text you write makes sense。 The role of the word processor is to give you  

the ability to construct intelligent text。 



Examining the Problem of Referencing Using Value Types 



As you’ve learned; a value type is stored on the stack; and its contents are copied; not refer

enced。 When you are trying to build a tree structure with a value type; references that were  

assigned are not updated with the correct information because values are copied。 This effect  

can be demonstrated by going through a longer example of building a data structure of cities  

that can be reached from another city。 To start off; consider the following declaration of all the  

cities and their coordinates。 


…………………………………………………………Page 114……………………………………………………………

92        CH AP T E R   4   ■    L E A R N IN G   AB OU T   D AT A  S TR U CT U R E S;   DE CI SI ON S;   A N D   L O OP S 



           Dim montreal As Node = New Node(〃Montreal〃; 0; 0) 

           Dim newyork As Node = New Node(〃New York〃; 0; …3) 

           Dim miami As Node = New Node(〃Miami〃; …1; …11) 

           Dim toronto As Node = New Node(〃Toronto〃; …4; …1) 

           Dim houston As Node = New Node(〃Houston〃; …10; …9) 

           Dim losangeles As Node = New Node(〃Los Angeles〃; …17; …6) 

           Dim seattle As Node = New Node(〃Seattle〃; …16; …1) 



                This code creates several Node objects that represent all of the cities from Figure 4…7。 The  

           individual objects are cities without connections; and the next step is to connect one city to  

           another。 We need to allocate and assign the Connections data member。  

                Having initialized all of the individual cities; the next step is to cross…reference the destina

           tions of each city。 In Visual Basic; the array requires some special understanding。 The Connections  

           data member is an empty array without any elements。 This is not a problem; as long as you  

           don’t attempt to reference or assign any of the elements。  

                You need to allocate space for the array so that you can store the individual cities。 One  

           solution is to reassign the array to another array that contains elements; as follows: 



           montreal。Connections = New Node() {newyork; toronto; losangeles} 



                In this example; the Connections data member is reassigned with the contents of another  

           array that has been allocated to contain the three cities; newyork; toronto; and losangeles。  

           Think of the array as a basket。 Using this approach; the original empty basket has been replaced  

           with a basket of three items。 These three items can be referenced using three indices (0 = newyork;  

           1 = toronto; 2 = losangeles)。 This solution creates a fixed…content basket。 

                Alternatively; you could create space in the basket and then manually fill the basket; as follows: 



           ReDim montreal。Connections(2)  

           montreal。Connections(0) = newyork 

           montreal。Connections(1) = toronto 

           montreal。Connections(2) = losangeles 



                The ReDim statement takes the array referenced by the data member Connections and real

           locates it to a new size。 That size could be larger or smaller than the current size。 In the example;  

           the size of the array is 2; which does not mean two elements; but rather that it is an array sized  

           from the index 0 to 2。  

                The ReDim statement actually performs a redimension of an array。 The example demon

           strates using it on an empty array; but you can also use it with an array that contains data; such  

           as to enlarge an array without losing the old data。 To keep the old data in a new array; you use  

           the Preserve keyword; as follows: 



           ReDim Preserve montreal。Connections(2)  

           montreal。Connections(0) = newyork 

           montreal。Connections(1) = toronto 

           montreal。Connections(2) = losangeles 



                However; be aware that if the ReDim statement causes an array to shrink; you can still lose  

           data—whatever elements are located above the array size are discarded。 To be safe; before  

           using ReDim; you might want to reference the array’s  Length property to verify that you are not  

           shrinking the array and losing data。 


…………………………………………………………Page 115……………………………………………………………

                       CH AP T E R   4   ■    L E A R N I N G   A B OU T   D AT A  S TR U CT U R E S;   DE CI SI ON S;   A N D   L O OP S 93 



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