VB str()函数有什么问题?

时间:2011-03-23 07:12:17

标签: vb6

str()或val()函数的异常行为让我的日子变得非常糟糕,以下错误让我有2天的时间找到它。

temp = Val(currentYear)
temp = temp + 1
nextYear = Str(temp)

MsgBox "YY" & currentYear & "YY"
MsgBox "YY" & nextYear & "YY"

currentYear的初始值为“2010” 结果应该是: YY2010YY YY2011YY

然而,结果是: YY2010YY YY 2011YY

这个错误很严重。之后,我在我的代码旁边添加一行:

temp = Val(currentYear)
temp = temp + 1
nextYear = Str(temp)
nextYear = LTrim(nextYear)
MsgBox "YY" & currentYear & "YY"
MsgBox "YY" & nextYear & "YY"

然后结果如预期的那样。有谁能解释一下?感谢。

1 个答案:

答案 0 :(得分:1)

您应该使用CStr代替。

Str使用空格表示它是正数,而CStr则不是。