vbscript - 由多余的分配修复的莫名其妙的崩溃
问题描述
我正在测试我为格式化时间跨度而编写的一个小片段(例如,last change 5m23s ago
),并不断收到我无法理解的错误。每次我尝试i
在调用中直接使用循环变量时ts()
,ASP 都会通知我An error occurred...
'the function
function ts(s)
dim m: m = CLng(s \ 60): s = s mod 60
dim h: h = CLng(m \ 60): m = m mod 60
ts = right("00" & h, 2) & "h" & _
right("00" & m, 2) & "m" & _
right("00" & s, 2) & "s"
end function
'the test
for i = 0 to 90000 step 15
' response.write i & ": " & ts(i) & "<br />" 'an error has occurred
dim j: j = i : response.write i & ": " & ts(j) & "<br />" 'works fine
next
这里到底发生了什么?
为什么ts(i)
每次都会产生错误?
鉴于此,为什么j=i : ts(j)
工作正常?
变量 不会有问题i
,因为它在 write 调用中可以正常工作。这是我尝试过的其他一些事情:
response.write i & ": " 'no problem
'response.write ts(i) 'crashes
'dim x: x = ts(i) 'crashes
dim j: j = i 'no problem
dim x: x = ts(j) 'works
response.write x & "<br />" 'works
'response.write ts(j) & "<br />" 'also works
'response.write i & ": " & ts(j) & "<br />" 'also works
(最后,我知道据说有一种方法可以让 IIS 显示真正的错误。我很想听听如何在没有 RDP 访问网络服务器的情况下做到这一点。)
解决方案
显然,在 VBScript 中,默认情况下是要传递参数ByRef
。
(从字面上看,我曾经使用过的所有其他编程语言都通过原语ByVal
ue)
当我更改s
函数内部的值时,这会导致问题。
这些片段中的任何一个都可以正常工作:
function ts(ByVal s)
...
...
ts(i)
function ts(sec)
dim s: s = sec
...
...
ts(i)
(或者,如 OP 中所述,将值传递给非循环迭代器变量)
function ts(s)
...
dim j: j = i: ts(j)
推荐阅读
- c# - Unity:无法在服务器构建中截取屏幕截图
- python - 从 epub 文件列表中创建一个 epub 文件(每个字符一个)
- java - 有没有办法让抛出的异常在执行其他任何操作之前先运行?
- json - Api GET 请求返回空响应,但邮递员没有
- python - 在 python 中使用 FIPY 计算联立偏微分方程
- c# - 加载包 TiberCreek.Toolkit.Common v 3.2.0.12 时出现问题
- java - JPA Hibertane 单向一对一与共享主键先保存父级 NO REVERSE
- python - 扩展列表理解
- html - 如何使用 CSS3 和 HTML5 将导航栏中的链接居中?
- python - TypeError:列表索引必须是整数或切片,而不是 NoneType - 如何修复以完成我的数据框?