Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement an array in vbscript with a variable size

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)
like image 334
Marshall Tigerus Avatar asked Jul 26 '13 16:07

Marshall Tigerus


2 Answers

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
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:

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.

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

like image 149
Ekkehard.Horner Avatar answered Oct 03 '22 23:10

Ekkehard.Horner


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

like image 39
Matt Burnside Avatar answered Oct 04 '22 00:10

Matt Burnside