热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在vbscript中实现一个可变大小的数组-Howtoimplementanarrayinvbscriptwithavariablesize

Iamredesigningapartofawebpagetomakeiteasiertoupdateinthefuture.Currently,itisas

I am redesigning a part of a webpage to make it easier to update in the future. Currently, it is a series of tables, that are hard-coded. To redesign the table (for example, to alphabetize it like I want to), requires manually swapping around a lot of values in the html.

我正在重新设计网页的一部分,以便将来更容易更新。目前,它是一系列硬编码的表。要重新设计表格(例如,按照我想要的方式按字母顺序排列),需要手动交换html中的大量值。

This is what I'd like to do: Create a url_Link object with a title and link variable, to hold the display name and the url respectively. Create an array of url_Link objects, and populate it at the top of the .asp file for the page. Perform a for each loop on those arrays to build and populate the table

这就是我想要做的:创建一个带有标题和链接变量的url_Link对象,分别保存显示名称和URL。创建一个url_Link对象数组,并将其填充到页面的.asp文件的顶部。对这些阵列上的每个循环执行a以构建和填充表

This itself isn't so bad, but I run into two problems. First, I'd like to not have to define the array size, as this makes a second place that has to be changed when changes are made to the number of links. There will be some logic to prevent certain url_Link objects from being displayed (for example, some users can't access certain pages, so they will not see links), and this would cause issues when sizing the arrays.

这本身并不是那么糟糕,但我遇到了两个问题。首先,我不想定义数组大小,因为当对链接数进行更改时,这是第二个必须更改的地方。将有一些逻辑来阻止显示某些url_Link对象(例如,某些用户无法访问某些页面,因此他们将看不到链接),这会在调整数组大小时引起问题。

I know that I could just make the arrays of a large size, but this seems wasteful to me (and I don't know how for each functions and do not want a bunch of empty rows to show up).

我知道我可以制作一个大尺寸的数组,但这对我来说似乎很浪费(而且我不知道每个函数如何,也不希望出现一堆空行)。

What can I do to resolve these problems? I'm not very knowledgeable in Vbscript, and most of the code that I have been working with does not take advantage of arrays or objects.

我该怎么做才能解决这些问题?我对Vbscript知之甚少,而且我一直在使用的大部分代码都没有利用数组或对象。

UPDATE: I've tried using a redim PRESERVE to trim the excess fat of an oversized array. The problem is that in some cases, my array is populated by smaller amounts of objects than its max size because of if conditions. This is causing problems later when I use a for loop (tried to get a for each to work and that is not happening at the moment). I get the error "This array is fixed or temporarily locked" on the redim line

更新:我尝试使用redim PRESERVE来修剪超大阵列的多余脂肪。问题是,在某些情况下,由于if条件,我的数组中填充的对象数量少于其最大大小。当我使用for循环时,这会导致问题(尝试为每个循环工作并且目前没有发生)。我在redim行上收到错误“此数组已固定或暂时锁定”

Code:

  dim systemSettingsArray(1) 
  arrayCounter = 0
  if ADMIN = "Y" then
      set systemSettingsArray(arrayCounter) = (new url_Link).Init("Account Administration","Maintenance/Account_Admin.asp")
      arrayCounter = arrayCounter + 1
  end if
  set systemSettingsArray(arrayCounter) = (new url_Link).Init("Time Approval","Maintenance/system_Time_Approval.asp")
  redim Preserve systemSettingsArray(arrayCounter)

2 个解决方案

#1


5  

Use redim preserve on the array. You can use UBound to find the current number of elements and do something like

在数组上使用redim preserve。您可以使用UBound查找当前的元素数量并执行类似操作

ReDim Preserve myArrayName (UBound(myArrayName) + 1)

http://msdn.microsoft.com/en-us/library/c850dt17%28v=vs.84%29.aspx

#2


3  

To show the correct way to use dynamic arrays in Vbscript and to prove Matt's comment wrong:

要显示在Vbscript中使用动态数组的正确方法,并证明Matt的注释错误:

Option Explicit

ReDim a(-1)
Dim b : b = Array()
Dim c()
Dim i
For i = 0 To 1
    ReDim Preserve a(UBound(a) + 1) : a(UBound(a)) = i
    ReDim Preserve b(UBound(b) + 1) : b(UBound(b)) = i
   On Error Resume Next
    ReDim Preserve c(UBound(c) + 1) : c(UBound(c)) = i
    WScript.Echo Err.Description, "- caused by Dim c()"
   On Error GoTo 0
Next
WScript.Echo "a:", Join(a)
WScript.Echo "b:", Join(b)

output:

Subscript out of range - caused by Dim c()
Subscript out of range - caused by Dim c()
a: 0 1
b: 0 1

Update wrt comment:

更新wrt评论:

Both the a and the b way are correct - you get an one dimensional dynamic array to which UBound() can be applied from the start. Some people may prefer b, because they don't like ReDim v without a previous Dim v; other may feel that b is clumsy or errorprone.

a和b方式都是正确的 - 你得到一个一维动态数组,从一开始就可以应用UBound()。有些人可能更喜欢b,因为他们不喜欢没有先前Dim v的ReDim v;其他人可能会觉得b是笨拙的或错误的。

If you look at this problem about a two-dimensional array, you may come to the conclusion, that the a way scales better.

如果你看一下关于二维数组的这个问题,你可能会得出结论,这种方式可以更好地扩展。


推荐阅读
author-avatar
mobiledu2502921033
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有