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

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

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





                The example is obvious and does nothing useful。 If you attempted to call the Object。 

          ToString() method on value; an exception would be generated。 So; what the code has created  

          is a variable value that references nothing and is nothing。 This works because the example uses  

          a  reference type。 Let’s see what happens when you assign a value to a value type that we  

          equate to no real value。 



          Dim value As Integer = 0 

                   

           If value = 0 Then 

              Console。WriteLine(〃Yup a Nothing???〃) 

           End If 



                This time; value is not of the type Object; but is of the type Integer。 And value is assigned  

          a value of 0 to indicate no real value。 Now many of you will be saying; “Hey; wait a minute— 

          0  Nothing。” Yet; in the programming languages C++ and C; Nothing and 0 have the same  

          contextual meaning; so it’s not so outlandish。 

               Visual Basic has a programming construct called the nullable type。 The nullable type solves the  

          problem of knowing whether or not a value type has been assigned。 This is needed when inter

          acting with relational databases。 For example; there is no way to indicate that an integer value  

          is not assigned; since declaring a value type means declaring a stack variable; and it will have a  

          default value。 In other words; there is no chance a value type will have a null reference。 

               A nullable type in Visual Basic is used to define a value…type variable that might have a null  

          state。 In the value type example; the integer value was assigned a value of 0; but a value of  

          Nothing would have been more appropriate。 However; value types cannot be assigned a value  

          of Nothing; they must be assigned some valid value。  

                To further demonstrate the restrictions of a value type; the following is a Structure  

          declaration。 


…………………………………………………………Page 449……………………………………………………………

                                 CH AP T E R   1 6   ■    L E A R N I N G   A B OU T   O TH E R   V IS U AL   B A SI C  T E C HN IQ U E S 427 



Structure ExampleStructure  

    Public value As Integer 

End Structure 



     Here’s how we might use it: 



Dim ex As ExampleStructure = Nothing 



     The structure ExampleStructure is defined using the Structure keyword; which means that  

ExampleStructure is a value type。 The next line; where the variable ex is assigned a value of  

Nothing; will result in an action where nothing happens other than resetting the variable ex to  

0。 And as was illustrated earlier; 0 is not  Nothing。  

     The inability to assign Nothing is odd; because structures behave similarly to reference  

types in many ways。 Instantiating a default value of a structure is not always the right answer;  

since the data members might not have a default value。 

     The nullable type makes it possible for value types (for example; structures) to be nothing。  

In Visual Basic; you define a nullable type by using the  generics class Nullable or the  

nullable type notation; as follows: 



Dim ex As Nullable(Of ExampleStructure) = Nothing 



or 



Dim ex As ExampleStructure? = Nothing 



     Using nullable types; it is possible to assign a Nothing value to a value type。 To assign a  

value to a nullable type; you need to explicitly assign the variable; as in the following example。 



ex = New ExampleStructure() With {。value = 10} 



     And then to reference the structure—in particular; the property value—use the following  

code。 



Console。WriteLine(〃(〃 & ex。Value。value & 〃)〃)   



     The additional reference of Value is the link between the reference type Nullable and the  

value type  ExampleStructure。 



■Note  Nullable types are not intended to be used in conjunction with reference types。 If you attempt to use  

a nullable type with a reference type; you will get a Visual Basic piler error。 Nullable types are designed  

for use by value types; and there is no other mystical or magical reason to use a nullable type。 



Partial Classes and Methods 



By default; whenever you create a type; you must declare the plete type in one file and as  

one entity。 For example; the following class would need to be declared in the context of a single file。 


…………………………………………………………Page 450……………………………………………………………

428       CH AP T E R   1 6   ■    L E A R N I N G   A B OU T   O TH E R   V IS U AL   B A SI C  TE C H N IQ U E S 



              Class Example  

                  Const BaseValue as Integer = 10 

                  Public Sub AddNumbers(ByVal value1 As Integer; _ 

                                        ByVal value2 As Integer; _ 

                                        ByRef response As Integer)  

                      response = BaseValue + value1 + value2 

                  End Sub 

              End Class 



               The Example class has a single method AddNumbers; and the method implementation adds  

          two numbers to a base value (BaseValue)。 Everything is in a single file。 But suppose you wanted  

          to automatically generate Example; and have it generate the data member BaseValue; but not  

          the method AddNumbers。 To do that; you would need to split the class into two separate classes;  

          where one class subclasses the other: 



              Class BaseGenerated  

                  Protected Const BaseValue As Integer = 10 

              End Class 



              Class Example : Inherits BaseGenerated  

                  Public Sub AddNumbers(ByVal value1 As Integer; _ 

                                        ByVal value2 As Integer; _ 

                                        ByRef response As Integer)  

                      response = BaseValue + value1 + value2 

                  End Sub 

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