本文介绍了(非1)由于某种原因求值为-2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么(非1)评估为-2?我希望它的评估结果为0.

Why does (Not 1) evaluate as -2? I would expect it to evaluate as 0.

推荐答案

VBA/VBScript没有真正的逻辑运算符(AND,OR,NOT).您看到的逻辑运算符实际上是按位运算符,仅此而已. VBA玩一些具有TrueFalse值的游戏,因此在大多数情况下都可以使用,但是有时您会发现陷阱".

VBA/VBScript does not have real logical operators (AND, OR, NOT). The logical operators you see are actually bitwise operators, and that's all you get. VBA plays some games with the True and False values so this works most of the time, but occasionally you'll find a "gotcha".

在这种情况下,您必须编写If InStr() <= 0 Then而不是If Not InStr() Then.
代替If InStr() Then,您必须编写If InStr() > 0 Then

In this case, instead of If Not InStr() Then you have to write If InStr() <= 0 Then.
Instead of If InStr() Then you have to write If InStr() > 0 Then

换句话说:InStr()返回一个数字.不要试图将其视为布尔值.

In other words: InStr() returns a number. Don't try to treat it like a boolean.

这篇关于(非1)由于某种原因求值为-2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 03:18