Take the 2-minute tour ×

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


  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)
share|improve this question
"Dim a(1)" creates a fixed (non-ReDim-able) array with 2 elements indexed 0 and 1. Use "Dim a : a = Array()" or "ReDim a(-1)" to create an empty ReDim-able array. –  Ekkehard.Horner Jul 26 '13 at 21:27
add comment

2 Answers

up vote 4 down vote accepted

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)


share|improve this answer
I tried this, and I get an error This array is fixed or temporarily locked. Code added above –  Marshall Tigerus Jul 26 '13 at 21:12
Marshall: Make sure that your array parentheses are empty when you initially dim the array variable. Check the remarks section in the link I posted, they mention it casually but it's kinda a huge deal. You will need to dim it with empty parens, and then immediately redim it. –  Matt Burnside Jul 26 '13 at 21:26
add comment

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

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
WScript.Echo "a:", Join(a)
WScript.Echo "b:", Join(b)


Subscript out of range - caused by Dim c()
Subscript out of range - caused by Dim c()
a: 0 1
b: 0 1
share|improve this answer
add comment

Your Answer


