ASP - 循环变量应该先声明再使用

今天遇到了一个错误

代码像这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%
dim dictionary : set dictionary = server.createObject("Scripting.Dictionary")
dictionary.add "size", "10"

for i = 0 to 10
call test(dictionary)
next

function test(param)
for i = 0 to param.count - 1
response.write param.keys()(i) & " => " & param.items()(i) & "<br>"
next
end function
%>

运行提示错误

1
2
3
4
5
6
7
Response 对象 错误 'ASP 0251 : 80004005'

超过响应缓冲区限制

/csv/add/test.asp,行 0

此 ASP 页的执行造成响应缓冲区超过其配置限制。

但是,我在函数中给循环变量i加了声明后

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%
dim dictionary : set dictionary = server.createObject("Scripting.Dictionary")
dictionary.add "size", "10"

for i = 0 to 10
call test(dictionary)
next

function test(param)
dim i ' 给循环变量添加声明
for i = 0 to param.count - 1
response.write param.keys()(i) & " => " & param.items()(i) & "<br>"
next
end function
%>

代码就可以正常打印了:

1
2
3
4
5
6
7
8
9
10
11
size => 10
size => 10
size => 10
size => 10
size => 10
size => 10
size => 10
size => 10
size => 10
size => 10
size => 10

搞不太明白怎么回事。还是尽量在使用循环变量前先添加声明吧。

当然,设置强制所有变量在使用前先声明选项就更好了

1
2
3
4
<%
' 在asp最顶部添加
option explicit on
%>