奇怪的方法asign变量

时间:2015-08-27 11:37:52

标签: vbscript

试图了解其他程序员代码

请说明为什么使用这种方法?

1. useradmin = user_Perms = "Admin" 'What it means and why it need asign "Admin" if user_Perms is already = "Admin"?
2. If 1=0 Then
3. If strText > 0 Then  ' How text string can be >0 ?

1 个答案:

答案 0 :(得分:2)

在VBScript中=用于分配比较。因此useradmin将被分配user_Perms与字符串文字"管理员"的内容比较的布尔结果。

证据

>> For Each user_Perms In Split("User Admin")
>>     useradmin = user_Perms = "Admin"
>>     WScript.Echo user_Perms, TypeName(useradmin), CStr(useradmin)
>> Next
>>
User Boolean False
Admin Boolean True

If 1=0 Then是一种稍微复杂的写作方式If False ThenThen分支中的代码永远不会被执行。

由于VBScript输入类型较弱且自动输入转换类型,strText > 0可能为True,False或您不想要的内容:

>> strText = "5"
>> WScript.Echo TypeName(strText), CStr(strText > 0)
>> strText = 5
>> WScript.Echo TypeName(strText), CStr(strText > 0)
>> strText = "-1"
>> WScript.Echo TypeName(strText), CStr(strText > 0)
>>
String True
Integer True
String False

这就是为什么你应该总是比较相同类型的值。

相关问题