Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 10477|回复: 4

Debug.Assert的作用是什么?

[复制链接]
发表于 2010-3-6 14:33 | 显示全部楼层 |阅读模式

Option Explicit
Private blnAssert As Boolean
Private intNumber As Integer

Private Sub Command1_Click()
    blnAssert = Not blnAssert
    intNumber = IIf(intNumber <> 0, 0, 1)
    Command1.Caption = intNumber
End Sub

Private Sub Command2_Click()
    Debug.Assert blnAssert
End Sub

Private Sub Form_Load()
    Command1.Caption = intNumber
    Command2.Caption = "Assert Tester"
End Sub

 

 

以上是帮助的代码,我觉得不对,所以改动了。看着改动后的附件,丝毫感觉不出Debug.Assert有啥用?

谢谢![em04]

H8yoFVlK.rar (8.6 KB, 下载次数: 6)
发表于 2010-3-6 17:04 | 显示全部楼层

跟Stop差不多吧,只是这个有条件的,而Stop是必须的。

你第一个按钮为0和为1时分别点第二个按钮,为0能中断代码,为1不中断吧

回复

使用道具 举报

发表于 2012-12-20 20:23 | 显示全部楼层
吕?布 发表于 2010-3-6 17:04
跟Stop差不多吧,只是这个有条件的,而Stop是必须的。你第一个按钮为0和为1时分别点第二个按钮,为0能中断代 ...

学习下              
回复

使用道具 举报

发表于 2012-12-20 20:50 | 显示全部楼层
Debug.Assert booleanexpression

booleanexpression  为false 时 程序被挂起,也就是暂停

用代码做测试

  1. Sub cc()
  2. Dim Hx As Integer
  3.   For Hx = 1 To 5
  4.     Debug.Assert Hx < 4
  5.     Debug.Print Hx
  6.   Next
  7. End Sub
复制代码
在本地窗口中可以看到  Debug.Print Hx 的值是  1,2,3
很明显 ,1,2,3, 都是 小于4 的,所以 前面一直是  true

而程序被挂起时, hx=4  ,4<4 这个条件式是不成立的,所以它返回的是 Fasle
这时候程序就被挂起了!!



回复

使用道具 举报

发表于 2012-12-20 20:58 | 显示全部楼层

其实他的效果用 stop 的方法来的话,和下面的一样

  1. Sub c()
  2. Dim Hx As Integer
  3.   For Hx = 1 To 5
  4.     If Hx > 3 Then Stop
  5.     Debug.Print Hx
  6.   Next
  7. End Sub
复制代码
更具体的区别是
Assert 调用只在开发环境中工作。当模块被编译成为一个可执行的文件时,调用 Debug 对象的方法就会被忽略。

如果在我们的可执行文件中使用  If Hx > 3 Then Stop  的话,则会提示 "遇到  Stop 语句"

这是在 VB 里写的两个程序代码

  1. Private Sub Form_Load()
  2. Dim Hx As Integer
  3.   For Hx = 1 To 5
  4.     If Hx > 3 Then Stop
  5.     Debug.Print Hx
  6.   Next
  7.   MsgBox "欢迎使用 Debug.Assert 测试程序"
  8. End Sub

  9. Private Sub Form_Load()
  10. Dim Hx As Integer
  11.   For Hx = 1 To 5
  12.     Debug.Assert Hx < 4
  13.     Debug.Print Hx
  14.   Next
  15.   MsgBox "欢迎使用 Debug.Assert 测试程序"
  16. End Sub
复制代码
这是对应的两个Exe 程序
Debug.Assert.zip (4.92 KB, 下载次数: 15)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-25 11:38 , Processed in 0.403825 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表