首页 > 解决方案 > 由多余的分配修复的莫名其妙的崩溃

问题描述

我正在测试我为格式化时间跨度而编写的一个小片段(例如,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 访问网络服务器的情况下做到这一点。)

标签: vbscriptasp-classic

解决方案


omegastripes 提示了我。

显然,在 VBScript 中,默认情况下是要传递参数ByRef
(从字面上看,我曾经使用过的所有其他编程语言都通过原语ByValue)

当我更改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) 

推荐阅读