如何在vbscript中实现一个可变大小的数组 [英] How to implement an array in vbscript with a variable size

查看:45
本文介绍了如何在vbscript中实现一个可变大小的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这本身还不错,但我遇到了两个问题.首先,我不想定义数组大小,因为这使得在更改链接数量时必须更改第二个位置.会有一些逻辑来阻止显示某些 url_Link 对象(例如,某些用户无法访问某些页面,因此他们将看不到链接),这会导致调整数组大小时出现问题.

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

我该怎么做才能解决这些问题?我对 vbscript 不是很了解,而且我使用的大部分代码都没有利用数组或对象.

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

代码:

 dim systemSettingsArray(1)数组计数器 = 0如果 ADMIN = "Y" 那么set systemSettingsArray(arrayCounter) = (new url_Link).Init("Account Administration","Maintenance/Account_Admin.asp")数组计数器 = 数组计数器 + 1万一set systemSettingsArray(arrayCounter) = (new url_Link).Init("Time Approval","Maintenance/system_Time_Approval.asp")redim 保留 systemSettingsArray(arrayCounter)

解决方案

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

的操作

ReDim 保留 myArrayName (UBound(myArrayName) + 1)

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

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.

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

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.

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.

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

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)

解决方案

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

ReDim Preserve myArrayName (UBound(myArrayName) + 1)

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

这篇关于如何在vbscript中实现一个可变大小的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆