4

I need to write some code in VBScript and have a version number string in a text file that I need to compare against. If I write this code as a test:

option explicit

Dim VersionString
VersionString = "6.2.1"

Dim Version
Version = CDbl (VersionString)

Version = Version * 100

I get an error on the CDbl line:

Microsoft VBScript runtime error: Type mismatch: 'CDbl'

How should I read and compare this string value?

4

2 回答 2

11

“6.2.1”不是格式化为字符串的 Double。所以 CDbl() 不能转换它。您的选择是:

  1. 将版本视为字符串;好的,如果您只需要比较是否相等,如果您需要“6.9.1”小于“6.10.2”,则不好
  2. 拆分()“。”上的字符串 并分别处理部分(可能转换为整数/长整数);您需要为此类数组编写一个比较函数
  3. 删除 "." 并 CLng 生成的字符串;对于像“6.10.2”这样的版本会中断
  4. 拆分()“*”上的字符串并乘以 + 添加“数字”以获得一个(整数)版本号(您的示例为 6 * 100 + 2 * 10 + 1 * 1 = 621);对于像“15.00.30729.01”这样的版本可能更复杂
于 2013-09-05T13:39:24.003 回答
1

转换为双精度无效,因为字符串中有两个小数点。要转换字符串,您必须删除其中一个或两个。

为此,您可以使用替换功能。替换的语法是

Replace(string, find, replacewith [, start [, count [, compare]]])

wherestring是要搜索的字符串,find是要查找replacewith的子字符串,是要替换的子字符串findstart是指定要开始搜索的索引count的可选参数,是指定要进行多少次替换的可选参数,以及compare是可选参数0 (vbBinaryCompare) 执行二进制比较,或 1 (vbTextCompare) 执行文本比较

' Remove all decimals
Version = CDbl(Replace(VersionString, ".", "")

' Remove only the first decimal
Version = CDbl(Replace(VersionString, ".", "", 1, 1)

' Remove only the second decimal
Version = CDbl(Replace(VersionString, ".", "", 3, 1)
于 2013-09-05T13:37:42.033 回答