VB6中“Null”和“Nothing”有什么区别?

前端之家收集整理的这篇文章主要介绍了VB6中“Null”和“Nothing”有什么区别?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个这样的记录集:
  1. Dim rs as Recordset
  2. Set rs as New Recordset
  3.  
  4. '... a lot of coding ...
  5.  
  6. if Err.Number <> 0 Then ' oops,something gone wrong!
  7. If rs.State <> adStateClosed Then rs.Close
  8. Set rs = Nothing
  9. end if
  10.  
  11. ' I want to evaluate if rs is Nothing,or Null
  12.  
  13. if rs is Nothing then
  14. ' this doesn't throw errors,and works well :D
  15. end if
  16.  
  17. if rs is Null then
  18. ' this throws an error of "types not compatible"
  19. end if
  20.  
  21. if rs = Null then
  22. ' this throws an error of "types not compatible"
  23. end if
  24.  
  25. if isNull(rs) then
  26. ' never enters here,isNull(rs) evaluates to False
  27. end if

我发现在VB6中我很少使用“Null”(我用它来评估空记录集模式名称),但我对图像,adodb.connections或记录集之类的东西使用“Nothing”.对于字符串我有vbNullString.我读到它是一个指向空字符串的指针.

“Null”是“未知变量值”而“Nothing”是真正的空值吗?

Null是Variant的特定子类型.它不存在于Variant类型之外,并且创建它以允许Variant为数据库null值建模.

没有什么是Object变量的值.它基本上与空指针相同,即没有对象.

以下引发错误,因为“Is”只能与Object变量一起使用:

  1. if rs is Null then
  2. ' this throws an error of "types not compatible"
  3. end if

以下引发错误,因为Object变量永远不能为空:

  1. if rs = Null then
  2. ' this throws an error of "types not compatible"
  3. end if

以下计算False,因为IsNull()采用Variant参数.

  1. if isNull(rs) then
  2. ' never enters here,isNull(rs) evaluates to False
  3. end if

它相当于:

  1. VarType(rs) = vbNull

猜你在找的VB相关文章