Null, Missing, Empty, Nothing, vbNullString的区别

vb6中存在几个虚幻的值:Null、Missing、Empty、Nothing、vbNullString。除了最后一个之外,每一个值都不能直接用“a=值”来判断。下面分别解释一下这几个值的含义。

1、Null。

Null指一个不合法的数据,判断一个变量是否为Null使用isNull函数。

这种数据通常出现在三种情况下:

(1)最简单的,函数直接返回Null给调用方。譬如

Function DivideEx(ByVal A as Double, ByVal B as Double) as Double

If B=0 Then DivideEx=Null Else DivideEx=A/B

End Function这个函数在B=0时返回Null指不合法数据。

(2)数据库中,当一个字段设为“允许空值”时,VB读取到空值就会用Null表示。譬如

Function GetCity(rst as ADODB.Recordset) as String

If isNull(rst.Field("City")) GetCity=rst.Field("Province") Else GetCity=rst.Field("City")

End Function在这个函数中,当City字段为空时(表示所在地区为一直辖市)返回Province字段,否则返回City字段。

(3)在调用库函数时,如果遇到传送变量类型与定义类型不一样时有时会出现Null值。

Null值在计算时有点奇怪,譬如Null-Null=Null,Null+10=10,Null+""=""等

2、Missing

Missing指传递进入Variant变量的缺少,判断Missing使用isMissing函数。譬如

Function test(Optional a)

If isMissing(a) Then test="You don't give this the varible a." Else test="You've given this the varible a."

End

Sub Main()

Debug.Print test '->You don't give this the varible a.

Debug.Print test(123) '->You've given this the varible a.

End Sub注意,Missing只会在Varient中出现,如果给入的数据类型是Byte,Integer,Long,Single,Double等缺少时为0;String缺少时为"";Object缺少时为Nothing。

3、Enpty

Empty指一个Variant变量未初始化,判断Empty使用isEmpty函数。譬如

Function test(a)

If isEmpty(a) Then test="a is Empty" Else test="a isn't Empty"

End Function

Sub Main()

Dim a as Variant

test(a) '->a is Empty

a=""

test(a) '->a isn't Empty

a=0

test(a) '->a isn't Empty

End Sub4、Nothing

Nothing相当于Object变量中的空值。指指向于空对象的引用。

未初始化的Object变量为Nothing;未传入函数的Object为Nothing。判断一个变量是否为Nothing使用 is Nothing表达式。

同时,Nothing还有另外一个用途,就是把所有指向某一个对象的Object变量赋值为Nothing可以销毁此对象节省内存空间。

5、vbNullString

vbNullString是一个String类型的常量,通常用于传递一个Null给库函数。不过很奇怪的是vbNullString=""这个表达式为True.  


FROM:http://gerhut.net/blogger/2006/08/vb6nullmissingemptynothingvbnullstring.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
修改下列代码:错误:类型不匹配:Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, lParam As Long) As Long Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExW" (ByVal hWndParent As Long, ByVal hWndChildAfter As Long, ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameW" (ByVal hWnd As Long, ByVal lpClassName As Long, ByVal nMaxCount As Long) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextW" (ByVal hWnd As Long, ByVal lpWindowText As Long, ByVal nMaxCount As Long) As Long Private Const WM_APPCOMMAND As Long = &H319 Private Const APPCOMMAND_VOLUME_UP As Long = &HA Private Const APPCOMMAND_VOLUME_DOWN As Long = &H9 Private Const APPCOMMAND_VOLUME_MUTE As Long = &H8 Public Sub SetSystemVolume(ByVal level As Integer) Dim hWndTaskbar As Long Dim hWndVolumeCtrl As Long Dim hWndParent As Long Dim hWndChild As Long Dim className As String hWndTaskbar = FindWindowEx(0, 0, "Shell_TrayWnd", vbNullString) If hWndTaskbar = 0 Then Exit Sub hWndVolumeCtrl = FindWindowEx(hWndTaskbar, 0, "TrayNotifyWnd", vbNullString) If hWndVolumeCtrl = 0 Then Exit Sub hWndParent = FindWindowEx(hWndVolumeCtrl, 0, "SysPager", vbNullString) If hWndParent = 0 Then Exit Sub hWndChild = FindWindowEx(hWndParent, 0, "ToolbarWindow32", vbNullString) If hWndChild = 0 Then Exit Sub ' get class name of the volume control className = Space(256) GetClassName hWndChild, StrPtr(className), Len(className) className = Left$(className, InStr(className, vbNullChar) - 1) ' find the volume control by window title hWndChild = FindWindowEx(hWndChild, 0, className, "Volume") If hWndChild = 0 Then Exit Sub SendMessage hWndChild, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_UP * &H10000 + level End Sub Public Sub MuteSystemVolume() Dim hWndTaskbar As Long Dim hWndVolumeCtrl As Long Dim hWndParent As Long Dim hWndChild As Long Dim className As String hWndTaskbar = FindWindowEx(0, 0, "Shell_TrayWnd", vbNullString) If hWndTaskbar = 0 Then Exit Sub hWndVolumeCtrl = FindWindowEx(hWndTaskbar, 0, "TrayNotifyWnd", vbNullString) If hWndVolumeCtrl = 0 Then Exit Sub hWndParent = FindWindowEx(hWndVolumeCtrl, 0, "SysPager", vbNullString) If hWndParent = 0 Then Exit Sub hWndChild = FindWindowEx(hWndParent, 0, "ToolbarWindow32", vbNullString) If hWndChild = 0 Then Exit Sub ' get class name of the volume control className = Space(256) GetClassName hWndChild, StrPtr(className), Len(className) className = Left$(className, InStr(className, vbNullChar) - 1) ' find the volume control by window title hWndChild = FindWindowEx(hWndChild, 0, className, "Volume") If hWndChild = 0 Then Exit Sub SendMessage hWndChild, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_MUTE * &H10000 End Sub Private Sub Command1_Click() MuteSystemVolume End Sub
最新发布
05-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值