ASP VBScript Redim Preserve类型不匹配 - 变量定义的范围

时间:2012-10-19 09:46:48

标签: asp-classic vbscript

我有一个经典的ASP页面,其中包含

部分中的服务器端
<!--#include file="../includes/DataTransferFunctions.asp"-->

在这个函数中,我在库文件的顶部有以下内容,

  Dim wsDataToXferArray()

然后,在库中的一个函数中,它会

 redim preserve wsDataToXferArray(3,pSub).

这不起作用,因为我在redim语句中遇到类型不匹配。但是,如果我将Dim语句放在主ASP的顶部而不是包含库的顶部,那么它就可以工作。

我需要能够在全局范围内声明变量,以便它可以在库中的多个函数中使用,但是要使它在库代码中定义,以便它是自包含的。我觉得好像缺少一些明显的东西。

感谢。

以下是显示问题的简化版本。我已经包含了主要的ASP和相关的库,它显示了“2 Dim”语句的位置。

感谢。

<%@ language = vbscript %>
<% Option Explicit %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%

Response.Buffer = true
Response.ContentType = "text/html"
Response.AddHeader "Content-Type", "text/html;charset=ISO-8859-1"
Response.CodePage = 1252
Response.CharSet = "ISO-8859-1"

Dim wsResult,wsDatabase
Dim wsSubX
'
'Dim wsDataToXferArray()

subRoutine()
'
'------------------------------------------------------------------------------------------------
sub subRoutine
'------------------------------------------------------------------------------------------------

    wsSubX  = 0
'
    if wsResult = "" then wsResult  = fncEnableSetAndCreateDataTransfer(wsDatabase,"LK-PART","abc","A",wsSubX)              :   wsSubX  = wsSubX + 1

End Sub

%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />
<title></title>
<%
SubIncludeOtherStyleSheets
SubIncludeJs
%>

<!--Include file containing functions to convert dates and to get sales order status description -->
<!--#include file="../includes/date_convert.asp"-->
<!--#include file="../includes/KHDataTransferFunctions.asp"-->
<!--#include file="../includes/IncludeStyleSheets.asp"-->
<!--#include file="../includes/IncludeJs.asp"-->

</head>

<body>

<form action="KHPartMaintenance.asp" method="post" name="form1" id="form1" >

<table class="tableForm Center Font7pt" width="1000">
    <thead>
    <tr>
        <th colspan="5">Part Maintenance</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td><input type="submit" class="submit1" name="submit1" id="btnList" value="Get Part" onclick="javascript:return fncFormOnSubmit('Get');" /></td>
    </tr>
    </tbody>
</table>

</body>
</html>

这是KHDataTransferFunctions.asp库。

<%
Dim wsDataToXferArray()

'------------------------------------------------------------------------------------------------
function fncEnableSetAndCreateDataTransfer(pDatabase,pName,pValue,pDataType,pSub)
'------------------------------------------------------------------------------------------------
'
Dim wsPrefix    :   wsPrefix = left(pName,2)
'
    fncEnableSetAndCreateDataTransfer   = ""

    call subAddFieldToDataTransferArray(wsPrefix,pName,pValue,pDataType,pSub)

end function

'------------------------------------------------------------------------------------------------
sub subAddFieldToDataTransferArray(pPrefix,pName,pValue,pDataType,pSub)
'------------------------------------------------------------------------------------------------
'
' Build the array of the fields for each 'transaction'.
'
    redim preserve wsDataToXferArray(3,pSub)

    wsDataToXferArray(0,pSub)   = pPrefix
    wsDataToXferArray(1,pSub)   = pName
    wsDataToXferArray(2,pSub)   = pValue
    wsDataToXferArray(3,pSub)   = pDataType
'   
End Sub

%>

我已经解决了这个问题。它是由ASP页面中包含文件的位置引起的。

这是经过修改的代码(好吧,部分内容......)。

<%@ language = vbscript %>
<% Option Explicit %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%

Response.Buffer = true
Response.ContentType = "text/html"
Response.AddHeader "Content-Type", "text/html;charset=ISO-8859-1"
Response.CodePage = 1252
Response.CharSet = "ISO-8859-1"
%>
<!--Include file containing functions to convert dates and to get sales order status description -->
<!--#include file="../includes/date_convert.asp"-->
<!--#include file="../includes/KHDataTransferFunctions.asp"-->
<!--#include file="../includes/IncludeStyleSheets.asp"-->
<!--#include file="../includes/IncludeJs.asp"-->
    <%
Dim wsResult,wsDatabase
Dim wsSubX
'
'Dim wsDataToXferArray()

subRoutine()
'
'------------------------------------------------------------------------------------------------
sub subRoutine
'------------------------------------------------------------------------------------------------

    wsSubX  = 0
'
    if wsResult = "" then wsResult  = fncEnableSetAndCreateDataTransfer(wsDatabase,"LK-PART","abc","A",wsSubX)              :   wsSubX  = wsSubX + 1

End Sub

%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />
<title></title>
<%
SubIncludeOtherStyleSheets
SubIncludeJs
%>

1 个答案:

答案 0 :(得分:1)

问题是由服务器端包含文件的位置引起的。这些需要在主ASP代码执行之前,以便足够早地声明任何全局变量。

请参阅解决方案原始帖子中包含修改后代码的块。