2006-11-10
自己打造一个ASP集合类
关键字: asp
在ASP开发中,往往苦于数据结构的贫乏,通常的做法是通过创建Scripting.Dictionary对象来得到一个集合类,但在有些情况下,Scripting.Dictionary组件被管理员禁用,我们会束手无策。最近我就碰到这样一个问题,我申请的一个免费ASP空间无法创建Scripting.Dictionary,无可奈何!
后来我想,能不能不依赖于服务器的组件,自己写一个真正无组件集合类呢?不动手怎么知道行不行呢,说干就干。
首先想到的是用数组来存储数据,但是VBScript中处理不定长数组比较麻烦,而且VBScript中的数组也不能按中字符串的键值来取值,于是决定用JScript来实现,怎么考虑的过程就不多说了,代码胜于一切描述:
<SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
//创建Map对象
function CreateMap() { return new Map(); }
//自定义的Map对象
function Map() {
this.Items = new Array();
this.Add = function(key, value) {
this.Items[key] = value;
}
this.Remove = function(key) {
this.Items[key] = null;
var tmpItems = new Array();
jj=0;
for(ii=0; ii<this.Items.length; ii++) {
if(this.Items[ii]==null) continue;
tmpItems[jj++] = this.Items[ii];
}
this.Items = tmpItems;
}
this.Len = function() {
return this.Items.length;
}
this.Get = function(key) {
return this.Items[key];
}
}
</SCRIPT>
调用时跟调用Scripting.Dictionary没有太大差别,例子如下:
<html>
<head>
<SCRIPT RUNAT="SERVER" LANGUAGE="JSCRIPT">
function CreateMap() { return new Map(); }
//自定义的Map对象
function Map() {
this.Items = new Array();
this.Add = function(key, value) {
this.Items[key] = value;
}
this.Remove = function(key) {
this.Items[key] = null;
var tmpItems = new Array();
jj=0;
for(ii=0; ii<this.Items.length; ii++) {
if(this.Items[ii]==null) continue;
tmpItems[jj++] = this.Items[ii];
}
this.Items = tmpItems;
}
this.Len = function() {
return this.Items.length;
}
this.Get = function(key) {
return this.Items[key];
}
}
</SCRIPT>
</head>
<body>
<%
Set Fields = CreateMap()
for ii=1 to 10
Fields.Add CStr(ii), "这是Item:" + CStr(ii)
Next ii
For Each Field In Fields.Items
Response.write Field + "<br>"
next
%>
</body>
</html>
后来我想,能不能不依赖于服务器的组件,自己写一个真正无组件集合类呢?不动手怎么知道行不行呢,说干就干。
首先想到的是用数组来存储数据,但是VBScript中处理不定长数组比较麻烦,而且VBScript中的数组也不能按中字符串的键值来取值,于是决定用JScript来实现,怎么考虑的过程就不多说了,代码胜于一切描述:
<SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
//创建Map对象
function CreateMap() { return new Map(); }
//自定义的Map对象
function Map() {
this.Items = new Array();
this.Add = function(key, value) {
this.Items[key] = value;
}
this.Remove = function(key) {
this.Items[key] = null;
var tmpItems = new Array();
jj=0;
for(ii=0; ii<this.Items.length; ii++) {
if(this.Items[ii]==null) continue;
tmpItems[jj++] = this.Items[ii];
}
this.Items = tmpItems;
}
this.Len = function() {
return this.Items.length;
}
this.Get = function(key) {
return this.Items[key];
}
}
</SCRIPT>
调用时跟调用Scripting.Dictionary没有太大差别,例子如下:
<html>
<head>
<SCRIPT RUNAT="SERVER" LANGUAGE="JSCRIPT">
function CreateMap() { return new Map(); }
//自定义的Map对象
function Map() {
this.Items = new Array();
this.Add = function(key, value) {
this.Items[key] = value;
}
this.Remove = function(key) {
this.Items[key] = null;
var tmpItems = new Array();
jj=0;
for(ii=0; ii<this.Items.length; ii++) {
if(this.Items[ii]==null) continue;
tmpItems[jj++] = this.Items[ii];
}
this.Items = tmpItems;
}
this.Len = function() {
return this.Items.length;
}
this.Get = function(key) {
return this.Items[key];
}
}
</SCRIPT>
</head>
<body>
<%
Set Fields = CreateMap()
for ii=1 to 10
Fields.Add CStr(ii), "这是Item:" + CStr(ii)
Next ii
For Each Field In Fields.Items
Response.write Field + "<br>"
next
%>
</body>
</html>
- 20:04
- 浏览 (499)
- 评论 (0)
- 分类: 技术文章(script&ASP)
- 相关推荐
发表评论
- 浏览: 89164 次

- 详细资料
搜索本博客
我的相册
谁敢范我
共 1 张
共 1 张
最近加入圈子
最新评论
-
Axis实践之Axis入门
楼主说的步骤太复杂了,初学者可能看得晕,这里有个捷径,web services和 ...
-- by taelons -
Web Service实践之——开 ...
用Spring的话 XFIRE还是首选
-- by lixiaoxu85 -
Web Service实践之——开 ...
wdmsyf 写道 Web Service实践之—&mdash ...
-- by fengzhiyin -
Axis实践之Axis入门
现在NetBeans下面也可以通过升级插件获得之间的Axis2集成了
-- by Joo -
Axis实践之Axis入门
第一种 我在eclipse 下通过测试 第二种 不行 Error genera ...
-- by xiaopang106






评论排行榜