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
小说推荐
- oracle从入门到精通(PDF格式)
- -Page 1-Oracle 从入门到精通-Page 2-资源来自网络,仅供学习 Oracle 从入门到精通一、SQL 8
- 其他
- 最新章:第37章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 其他
- 最新章:第4章
- Java编程思想第4版[中文版](PDF格式)
- -Page 1-Page 2《Thinking In Java》中文版作者:Bruce Eckel主页:http/BruceEckel.编译:Trans Bot主页:http/memberease~transbot致谢-献给那些直到现在仍在孜孜不倦创造下一代计算机语言的人们!指导您利用万维网的语言进
- 其他
- 最新章:第295章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 其他
- 最新章:第309章
- VC语言6.0程序设计从入门到精通
- -Page 1-Visual C 6.0 程序设计从入门到精通求是科技 王正军 编著
- 其他
- 最新章:第136章
- SQL 21日自学通(V3.0)(PDF格式)
- -Page 1-SQL 21 日自学通(V1.0 翻译人 笨猪目录目录 1译者的话 14第一周概貌 16从这里开始 16
- 其他
- 最新章:第170章
- 2008年青年文摘精编版
- 作者:中国青年出版社“初恋”的惩罚.作者:凡 凡 文章来源《真情》2005年第4期 点击数:6608 更新时间:2005-6-5过了年,我就十八岁了。离高考只剩下四个多月了。这一段,班里的男女生相互间递纸条、写情书、约会等地下活动慢慢的多了起来。我这个“尖子生”也突然感到了不安、慌乱,并且自责。不知
- 文学名著
- 最新章:第230章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 其他
- 最新章:第27章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 其他
- 最新章:第28章