Null, Empty, Nothing, and vbNullString的区别

相信大家在做系统的时候会经常遇到null、empty、nothing、vbnullstring这一类变量,他们有很大的相似性,以至于我们在选择的时候会有些混乱。据此,通过整理有关资料在这里把这些变量的区别总结一下,有哪里不完善的欢迎提出。
Null:Null是一个象Integer或者String一样的变量类型,它表示一个没有合法数据的变量。这有别于zero、Nothing、Empty或者vbNullString。许多数值与Null结合在一起,都将产生Null结果。比如:   表达式 结果  Null - Null Null  Null + 7 7  Null = Null Null   你可以使用IsNull语句来判断表达式是否为Null:   If IsNull(my_variable) Then ...  
Empty:这也是一个象Integer或者String一样的变量类型,它表示了一个还没有进行初始化的变量。它与Null的意义不同,Null表示没有合法数据。   一个没有初始化的变量的数值是Empty。你可以使用IsEmpty语句来判断是否变量进行了初始化:   If IsEmpty(my_variant) Then ...  
Nothing:这是一个指向空对象的对象引用。将对象引用设置为Nothing,就释放了那个对象。如果没有其他的引用指向对象,Visual Basic就将销毁这个对象。   Dim obj As Form1  :  Set obj = Nothing 注释: Free the object reference.   使用Is Nothing语句来判断是否一个引用为Nothing:   If obj Is Nothing Then ...  
vbNullString:这是个常量,表示一个empty字符串。它与空白字符串""不同,表示什么也没有的字符串(nothing string)。对于许多场合,它被当作一个empty字符串""处理,真正使用它的目的是传递null参数给库函数。  判断字符串是否为空白   
有以下几种方法判断一个字符串是否为空白:   Dim txt As String  Dim blank As String   blank = ""  :  If Len(txt) = 0 Then ...  If txt = vbNullString Then ...  If txt = "" Then ...  If txt = blank Then ...   经过测试,Len(txt)=0的方法要比其他方法快20%多


修改下列代码:错误:类型不匹配: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
评论 34
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值