开发者

How can I assert in VBScript scripts?

What is a good way to use asserts in VBScript scripts?

Is there built-in functionality for it or will it have to be emulated? What is best practice?

One application is to test for objects being Nothing duri开发者_如何学Gong development.


An operational answer (for others that may need it) is to define this function, from Rosetta Code:

    sub Assert( boolExpr, strOnFail )
        if not boolExpr then
            Err.Raise vbObjectError + 99999, , strOnFail
        end if
    end sub

Invocation:

    Set obj2 = Nothing
    Assert Not obj2 Is Nothing, "obj2 is Nothing!"

Output:

    someScript.vbs(17, 3) (null): obj2 is Nothing!


Sadly, I don't believe VBScript has anything built-in. You'll need to define your own Assert method and probably use a pre-processor build script or something to remove them when building a release copy of your script. (Actually removing -- at least commenting out -- the Assert call is best, rather than just making the body of the Assert not do anything in the released code.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜