返回列表 回复 发帖

解决在不支持FSO空间中无法使用Scripting.Dictionary对象的方法

解决在不支持FSO空间中无法使用Scripting.Dictionary对象的方法

前些天装了一个 IT学习者网站访问统计系统 在本机调试一切正常后传到空间却显示服务器内部错误,为弄明白什么错误,我到 IE工具→Internet选项→高级→取消 显示友好HTTP 错误信息,然后刷新后显示了错误原因:
Microsoft VBScript 运行时错误 错误 ';800a01ad';
ActiveX 部件不能创建对象: ';Scripting.Dictionary';
/languages.asp,行 29
languages.asp原程序
  1.    
  2. <%
  3. Dim SelectedTimeZone,SelectedLanguage
  4. if request.form("timeZone")<>"" then
  5. SelectedTimeZone = request.form("timeZone")
  6. response.cookies("TimeZone") = SelectedTimeZone
  7. else
  8. if request.cookies("TimeZone")="" then
  9. SelectedTimeZone = TimeZone
  10. response.cookies("TimeZone") = TimeZone
  11. else
  12. SelectedTimeZone = request.cookies("TimeZone")
  13. end if
  14. end if
  15. if request.form("Language")<>"" then
  16. SelectedLanguage = request.form("Language")
  17. response.cookies("Language") = SelectedLanguage
  18. else
  19. if request.cookies("Language") = "" then
  20. SelectedLanguage = Language
  21. response.cookies("Language") = SelectedLanguage
  22. else
  23. SelectedLanguage = request.cookies("Language")
  24. end if
  25. end if
  26. Dim Lang
  27. Set Lang = CreateObject("Scripting.Dictionary")
  28. select case SelectedLanguage
  29. case "CHS"
  30. ';Chinese Simplified
  31. %>
  32. <!-- &#35;include file="language/CHS.asp"-->
  33. <%
  34. case "CHT"
  35. ';Chinese Traditional
  36. %>
  37. <!-- &#35;include file="language/CHT.asp"-->
  38. <%
  39. case "ENG"
  40. ';English
  41. %>
  42. <!-- &#35;include file="language/ENG.asp"-->
  43. <%
  44. end select
  45. function clearLanguage()
  46. on error resume next
  47. clearLanguage   = Lang.removeAll   
  48. set clearLanguage  = nothing
  49. end function
  50. %>
复制代码
我找到languages.asp,行 29 这里的语句是Set Lang = CreateObject("Scripting.Dictionary"),显然CreateObject("Scripting.Dictionary")这个是必需FSO支持的,而我的空间刚好不支持FSO,于是我明白了为什么这个程序不能运行的原因了,但是我非常想使用这个统计系统,不禁抱怨一个小小的统计系统还要FSO支持,真郁闷!根据自己所学,当然也查了些资料,于是便想到Dictionary 对象等价于 PERL 联合数组。项目可以是数据的任何形式,并存储在数组中。每个项目都与一个具有唯一性的键相联。该键用于取得单个项目,并且通常是整数或字符串,但也可以是除数组以外的任何类型。
     根据这个"模版类"的功能,我运用我所学分析,应该可以不用FSO支持而用一个“字典类”来同Scripting.Dictionary等价的,于是遍写了下面的一个"字典类"。
     此类同Scripting.Dictionary对象的使用没有任何的区别,所以以前是根据Scripting.Dictionary来写的程序,不用怎样的修改就可以使用到此类上.此类并且还比Scripting.Dictionary多了一个Insert方法:Insert(sKey,nKey,nval,nMethod),此方法是将新字典数据插入到存在的以sKey为Key的字典位置.nKey,nVal是新字典数据的Key值和Value值.nMethod则是插入的位置.如果此值为1,"b","black"或空值,则是插入到以sKey为Key的字典数据后面,否则前面.
代码如下:
  1.    
  2. Class DictionaryClass
  3. Dim ArryObj()     ';使用该二维数组来做存放数据的字典
  4. Dim MaxIndex       ';MaxIndex则是ArryObj开始的最大上标
  5. Dim CurIndex       ';字典指针,用来指向ArryObj的指针
  6. Dim C_ErrCode       ';错误代码号
  7. Private Sub Class_Initialize
  8. CurIndex=0       ';从下标0开始
  9. C_ErrCode=0       ';0表示没有任何错误
  10. MaxIndex=50       ';默认的大小
  11. Redim ArryObj(1,MaxIndex)   ';定义一个二维的数组
  12. End Sub
  13. Private Sub Class_Terminate
  14. Erase ArryObj   ';清除数组
  15. End Sub
  16. Public Property Get ErrCode ';返回错误代码
  17.   ErrCode=C_ErrCode
  18. End Property
  19. Public Property Get Count   ';返回数据的总数,只返回CurIndex当前值-1即可.
  20.   Count=CurIndex
  21. End Property
  22. Public Property Get Keys   ';返回字典数据的全部Keys,返回数组.
  23.   Dim KeyCount,ArryKey(),I
  24.   KeyCount=CurIndex-1
  25.   Redim ArryKey(KeyCount)
  26.   For I=0 To KeyCount
  27.     ArryKey(I)=ArryObj(0,I)
  28.   Next
  29.   Keys=ArryKey
  30.   Erase ArryKey
  31. End Property
  32. Public Property Get Items   ';返回字典数据的全部Values,返回数组.
  33.   Dim KeyCount,ArryItem(),I
  34.   KeyCount=CurIndex-1
  35.   Redim ArryItem(KeyCount)
  36.   For I=0 To KeyCount
  37.     If isObject(ArryObj(1,I)) Then
  38.       Set ArryItem(I)=ArryObj(1,I)
  39.   Else
  40.     ArryItem(I)=ArryObj(1,I)
  41.   End If
  42.   Next
  43.   Items=ArryItem
  44.   Erase ArryItem
  45. End Property
  46. Public Property Let Item(sKey,sVal) ';取得sKey为Key的字典数据
  47.   If sIsEmpty(sKey) Then
  48.   Exit Property
  49.   End If
  50.   Dim i,iType
  51.   iType=GetType(sKey)
  52.   If iType=1 Then ';如果sKey为数值型的则检查范围
  53.   If sKey>CurIndex Or sKey<1 Then
  54.   C_ErrCode=2
  55.     Exit Property
  56. End If
  57.   End If
  58.   If iType=0 Then
  59.   For i=0 to CurIndex-1
  60.     If ArryObj(0,i)=sKey Then
  61.     If isObject(sVal) Then
  62.       Set ArryObj(1,i)=sVal
  63.   Else
  64.     ArryObj(1,i)=sVal
  65.   End If
  66.   Exit Property
  67.   End If
  68.   Next
  69.   ElseIf iType=1 Then
  70.       sKey=sKey-1
  71.     If isObject(sVal) Then
  72.       Set ArryObj(1,sKey)=sVal
  73.   Else
  74.     ArryObj(1,sKey)=sVal
  75.   End If
  76.   Exit Property
  77.   End If
  78.   C_ErrCode=2         ';ErrCode为2则是替换或个为sKey的字典数据时找不到数据
  79. End Property
  80. Public Property Get Item(sKey)
  81.   If sIsEmpty(sKey) Then
  82.     Item=Null
  83.   Exit Property
  84.   End If
  85.   Dim i,iType
  86.   iType=GetType(sKey)
  87.   If iType=1 Then ';如果sKey为数值型的则检查范围
  88.   If sKey>CurIndex Or sKey<1 Then
  89.     Item=Null
  90.   Exit Property
  91. End If
  92.   End If
  93.   If iType=0 Then
  94.   For i=0 to CurIndex-1
  95.     If ArryObj(0,i)=sKey Then
  96.     If isObject(ArryObj(1,i)) Then
  97.       Set Item=ArryObj(1,i)
  98.   Else
  99.     Item=ArryObj(1,i)
  100.   End If
  101.   Exit Property
  102.   End If
  103.   Next
  104.   ElseIf iType=1 Then
  105.       sKey=sKey-1
  106.     If isObject(ArryObj(1,sKey)) Then
  107.       Set Item=ArryObj(1,sKey)
  108.   Else
  109.     Item=ArryObj(1,sKey)
  110.   End If
  111.   Exit Property
  112.   End If
  113.   Item=Null
  114. End Property
  115. Public Sub Add(sKey,sVal) ';添加字典
  116.   ';On Error Resume Next
  117.   If Exists(sKey) Or C_ErrCode=9 Then
  118.   C_ErrCode=1           ';Key值不唯一(空的Key值也不能添加数字)
  119.   Exit Sub
  120. End If
  121.   If CurIndex>MaxIndex Then
  122.   MaxIndex=MaxIndex+1       ';每次增加一个标数,可以按场合需求改为所需量
  123.   Redim Preserve ArryObj(1,MaxIndex)
  124. End If
  125. ArryObj(0,CurIndex)=Cstr(sKey)     ';sKey是标识值,将Key以字符串类型保存
  126. if isObject(sVal) Then
  127.   Set ArryObj(1,CurIndex)=sVal     ';sVal是数据
  128. Else
  129.   ArryObj(1,CurIndex)=sVal     ';sVal是数据
  130. End If
  131. CurIndex=CurIndex+1
  132. End Sub
  133. Public Sub Insert(sKey,nKey,nVal,sMethod)
  134. If Not Exists(sKey) Then
  135.   C_ErrCode=4
  136.   Exit Sub
  137. End If
  138.   If Exists(nKey) Or C_ErrCode=9 Then
  139.   C_ErrCode=4           ';Key值不唯一(空的Key值也不能添加数字)
  140.   Exit Sub
  141. End If
  142. sType=GetType(sKey)         ';取得sKey的变量类型
  143. Dim ArryResult(),I,sType,subIndex,sAdd
  144. ReDim ArryResult(1,CurIndex)   ';定义一个数组用来做临时存放地
  145. if sIsEmpty(sMethod) Then sMethod="b"   ';为空的数据则默认是"b"
  146. sMethod=lcase(cstr(sMethod))
  147. subIndex=CurIndex-1
  148. sAdd=0
  149. If sType=0 Then             ';字符串类型比较
  150.   If sMethod="1" Or sMethod="b" Or sMethod="back" Then ';将数据插入sKey的后面
  151.     For I=0 TO subIndex
  152.       ArryResult(0,sAdd)=ArryObj(0,I)
  153.   If IsObject(ArryObj(1,I)) Then
  154.     Set ArryResult(1,sAdd)=ArryObj(1,I)
  155.   Else
  156.     ArryResult(1,sAdd)=ArryObj(1,I)
  157.   End If
  158.   If ArryObj(0,I)=sKey Then ';插入数据
  159.     sAdd=sAdd+1
  160.     ArryResult(0,sAdd)=nKey
  161.   If IsObject(nVal) Then
  162.     Set ArryResult(1,sAdd)=nVal
  163.   Else
  164.     ArryResult(1,sAdd)=nVal
  165.   End If
  166.   End If
  167.   sAdd=sAdd+1
  168.   Next
  169.   Else
  170.     For I=0 TO subIndex
  171.   If ArryObj(0,I)=sKey Then ';插入数据
  172.     ArryResult(0,sAdd)=nKey
  173.   If IsObject(nVal) Then
  174.     Set ArryResult(1,sAdd)=nVal
  175.   Else
  176.     ArryResult(1,sAdd)=nVal
  177.   End If
  178.   sAdd=sAdd+1
  179.   End If
  180.   ArryResult(0,sAdd)=ArryObj(0,I)
  181.   If IsObject(ArryObj(1,I)) Then
  182.     Set ArryResult(1,sAdd)=ArryObj(1,I)
  183.   Else
  184.     ArryResult(1,sAdd)=ArryObj(1,I)
  185.   End If
  186.   sAdd=sAdd+1
  187.   Next
  188.   End If
  189. ElseIf sType=1 Then
  190.   sKey=sKey-1             ';减1是为了符合日常习惯(从1开始)
  191.   If sMethod="1" Or sMethod="b" Or sMethod="back" Then ';将数据插入sKey的后面
  192.     For I=0 TO sKey         ';取sKey前面部分数据
  193.       ArryResult(0,I)=ArryObj(0,I)
  194.   If IsObject(ArryObj(1,I)) Then
  195.     Set ArryResult(1,I)=ArryObj(1,I)
  196.   Else
  197.     ArryResult(1,I)=ArryObj(1,I)
  198.   End If
  199.   Next
  200. ';插入新的数据
  201. ArryResult(0,sKey+1)=nKey
  202. If IsObject(nVal) Then
  203.   Set ArryResult(1,sKey+1)=nVal
  204. Else
  205.   ArryResult(1,sKey+1)=nVal
  206. End If
  207. ';取sKey后面的数据
  208.     For I=sKey+1 TO subIndex
  209.       ArryResult(0,I+1)=ArryObj(0,I)
  210.   If IsObject(ArryObj(1,I)) Then
  211.     Set ArryResult(1,I+1)=ArryObj(1,I)
  212.   Else
  213.     ArryResult(1,I+1)=ArryObj(1,I)
  214.   End If
  215.   Next
  216.   Else
  217.     For I=0 TO sKey-1         ';取sKey-1前面部分数据
  218.       ArryResult(0,I)=ArryObj(0,I)
  219.   If IsObject(ArryObj(1,I)) Then
  220.     Set ArryResult(1,I)=ArryObj(1,I)
  221.   Else
  222.     ArryResult(1,I)=ArryObj(1,I)
  223.   End If
  224.   Next
  225. ';插入新的数据
  226. ArryResult(0,sKey)=nKey
  227. If IsObject(nVal) Then
  228.   Set ArryResult(1,sKey)=nVal
  229. Else
  230.   ArryResult(1,sKey)=nVal
  231. End If
  232. ';取sKey后面的数据
  233.     For I=sKey TO subIndex
  234.       ArryResult(0,I+1)=ArryObj(0,I)
  235.   If IsObject(ArryObj(1,I)) Then
  236.     Set ArryResult(1,I+1)=ArryObj(1,I)
  237.   Else
  238.     ArryResult(1,I+1)=ArryObj(1,I)
  239.   End If
  240.   Next
  241.   End If
  242. Else
  243.   C_ErrCode=3
  244.   Exit Sub
  245. End If
  246. ReDim ArryObj(1,CurIndex) ';重置数据
  247. For I=0 To CurIndex
  248. ArryObj(0,I)=ArryResult(0,I)
  249. If isObject(ArryResult(1,I)) Then
  250.   Set ArryObj(1,I)=ArryResult(1,I)
  251. Else
  252.   ArryObj(1,I)=ArryResult(1,I)
  253. End If
  254. Next
  255. MaxIndex=CurIndex
  256. Erase ArryResult
  257. CurIndex=CurIndex+1     ';Insert后数据指针加一
  258. End Sub
  259. Public Function Exists(sKey)   ';判断存不存在某个字典数据
  260.   If sIsEmpty(sKey) Then
  261.     Exists=False
  262.     Exit Function
  263. End If
  264. Dim I,vType
  265. vType=GetType(sKey)
  266. If vType=0 Then
  267.   For I=0 To CurIndex-1
  268.   If ArryObj(0,I)=sKey Then
  269.   Exists=True
  270.   Exit Function
  271. End If
  272.   Next
  273. ElseIf vType=1 Then
  274.     If sKey<=CurIndex And sKey>0 Then
  275.     Exists=True
  276.     Exit Function
  277.   End If
  278. End If
  279. Exists=False
  280. End Function
  281. Public Sub Remove(sKey)         ';根据sKey的值Remove一条字典数据
  282. If Not Exists(sKey) Then
  283.   C_ErrCode=3
  284.   Exit Sub
  285. End If
  286. sType=GetType(sKey)         ';取得sKey的变量类型
  287. Dim ArryResult(),I,sType,sAdd
  288. ReDim ArryResult(1,CurIndex-2)   ';定义一个数组用来做临时存放地
  289. sAdd=0
  290. If sType=0 Then             ';字符串类型比较
  291.     For I=0 TO CurIndex-1
  292.   If ArryObj(0,I)<>sKey Then
  293.       ArryResult(0,sAdd)=ArryObj(0,I)
  294.   If IsObject(ArryObj(1,I)) Then
  295.     Set ArryResult(1,sAdd)=ArryObj(1,I)
  296.   Else
  297.     ArryResult(1,sAdd)=ArryObj(1,I)
  298.   End If
  299.   sAdd=sAdd+1
  300. End If
  301.   Next
  302. ElseIf sType=1 Then
  303.   sKey=sKey-1             ';减1是为了符合日常习惯(从1开始)
  304.     For I=0 TO CurIndex-1
  305.   If I<>sKey Then
  306.       ArryResult(0,sAdd)=ArryObj(0,I)
  307.   If IsObject(ArryObj(1,I)) Then
  308.     Set ArryResult(1,sAdd)=ArryObj(1,I)
  309.   Else
  310.     ArryResult(1,sAdd)=ArryObj(1,I)
  311.   End If
  312.   sAdd=sAdd+1
  313. End If
  314.   Next
  315. Else
  316.   C_ErrCode=3
  317.   Exit Sub
  318. End If
  319. MaxIndex=CurIndex-2
  320. ReDim ArryObj(1,MaxIndex) ';重置数据
  321. For I=0 To MaxIndex
  322. ArryObj(0,I)=ArryResult(0,I)
  323. If isObject(ArryResult(1,I)) Then
  324.   Set ArryObj(1,I)=ArryResult(1,I)
  325. Else
  326.   ArryObj(1,I)=ArryResult(1,I)
  327. End If
  328. Next
  329. Erase ArryResult
  330. CurIndex=CurIndex-1     ';减一是Remove后数据指针
  331. End Sub
  332. Public Sub RemoveAll ';全部清空字典数据,只Redim一下就OK了
  333.   Redim ArryObj(MaxIndex)
  334. CurIndex=0
  335. End Sub
  336. Public Sub ClearErr   ';重置错误
  337.   C_ErrCode=0
  338. End Sub
  339. Private Function sIsEmpty(sVal) ';判断sVal是否为空值
  340.   If IsEmpty(sVal) Then
  341.   C_ErrCode=9           ';Key值为空的错误代码
  342.   sIsEmpty=True
  343.   Exit Function
  344. End If
  345.   If IsNull(sVal) Then
  346.   C_ErrCode=9           ';Key值为空的错误代码
  347.   sIsEmpty=True
  348.   Exit Function
  349. End If
  350.   If Trim(sVal)="" Then
  351.   C_ErrCode=9           ';Key值为空的错误代码
  352.   sIsEmpty=True
  353.   Exit Function
  354. End If
  355. sIsEmpty=False
  356. End Function
  357. Private Function GetType(sVal)   ';取得变量sVal的变量类型
  358.   dim sType
  359. sType=TypeName(sVal)
  360.   Select Case sType
  361.   Case "String"
  362.     GetType=0
  363.   Case "Integer","Long","Single","Double"
  364.     GetType=1
  365.   Case Else
  366.     GetType=-1
  367. End Select
  368. End Function
  369. End Class
复制代码
   
';/*用法:
';/*Dim objDic,sKey,I,sValue
';/*Set objDic=New DictionaryClass
';/*Add方法:Add(字典的Key值,字典数据)   说明:如果"字典的Key值"已存在则Add方法失败
';/*objDic.Add "a","字母a"     ';Add方法
';/*objDic.Add "b","字母b"   
';/*objDic.Add "c","字母c"
';/*';Insert方法:Insert(被插入位置的Key值,新的字典Key值,新的字典数据,插入方式:b后面,f前面)
';/*objDic.Insert "a","aa","字母aa","b"   
';/*objDic.Insert "b","bb","字母bb","f"
';/*';Exists方法,返回是否存在以"b"为Key值的字典数据
';/*Response.Write objDic.Exists("b")
';/*sKey=objDic.Keys         ';获取Keys集合,(数组集合)
';/*sValue=objDic.Items       ';获取字典数据集合(数组集合)
';/*objDic.Item("a")="aaaaaa"   ';Item属性方法:返回或设置对应Key的字典数据
';/*For I=0 To objDic.Count-1   ';Count属性返回有多少条字典数据
';/*   ';Item属性方法:返回或设置对应Key的字典数据
';/*   Response.Write objDic.Item(sKey(I))&"<br>"
';/*Next
';/*Remove方法:Remove(字典的Key值)
';/*objDic.Remove("a")       ';删除Key值为a的字典数据
';/*objDic.RemoveAll         ';清空字典数据
';/*objDic.ErrCode           ';返回操作字典时的一些错误代码(调试时用)
';/*objDic.ClearErr         ';清空错误代码(调试时用)
';/*Set objDic=nothing
';/*说明:
';/*"字典的Key值":除了Add方法外,都可以用字符串或序数(1,2..)使用  
于是我在程序中用这个"字典类"取代CreateObject("Scripting.Dictionary")后在上传,运行,终于搞定程序运行正常!终于可以用这统计了,不过成功之余也不禁感叹,没有FSO的支持,要实现短短的一句CreateObject("Scripting.Dictionary")的功能是多么的麻烦,也可见FSO在程序中的重要性了,很多时候不要FSO支持一个简单的程序也将变的冗长无比!
顺便也提供这个统计系统的原版下载以供大家参考研究或者使用
原始下载:http://www.itlearner.com/114down/s22/21492.shtml
我修改后的可以在没有FSO支持的空间运行的程序下载:
http://count.xqin.com/count.rar(后台:用户:xqin.com密码:xqin.com)
演示:http://count.xqin.com
下面是这个统计系统中我修改后可以在没有FSO支持的空间中运行的languages.asp程序:欢迎大家交流,共同提高!
http://count.xqin.com/languages.txt

                     我是一个呼吸着现在的空气而生活在过去的人
               这样的注定孤独,孤独的身处闹市却犹如置身于荒漠
                                     我已习惯了孤独,爱上孤独
                                 他让我看清了自我,还原了自我
                             让我再静静的沉思中得到快乐和满足
                                   再孤独的世界里我一遍又一遍
                                   不厌其烦的改写着自己的过去
                                             延伸到现在与未来
                                       然而那只是泡沫般的美梦
                                 产生的时刻又伴随着破灭的到来
                         在灰飞烟灭的瞬间我看到的是过程的美丽
                                      而不是结果的悲哀。。。
返回列表