If you are using VBScript for scripting or in a classic ASP page adding items to an array is a difficult and slow process.
The arraylist class from .net is accessible via com and it provides a number of advantages over the native VBScript functionality:
- It requires less code
- It is faster
- It makes it simple to sort arrays
- Adding and removing item is easy
- No need to re-dimension arrays
Example Code:
Set players = CreateObject("System.Collections.ArrayList") players.Add "Novak Djokovic" players.Add "Rafael Nadal" players.Add "Roger Federer" players.Add "Dominic Thiem" players.Sort WScript.Echo Replace("There are {0} players in the draw", "{0}", players.Count)
You can easily loop through each items in the array like this:
For Each player In players WScript.Echo player Next
or like this:
For i = 0 To players.Count - 1 If i < players.Count - 1 Then WScript.Echo players(i) Else WScript.echo Replace("The last player is {0}", "{0}", players(i)) End if Next
I used this technique on a script I wrote for a customer around 10 years ago. The script still worked fine but it was slow as it was working through a lot of data. By changing the script to use the Arraylist class I was able to reduce the amount of code and increased the performance by around 50%.
It’s also easy to remove items from a VBScript array created this way:
players.Remove "Dominic Thiem"
You can also use the arraylist class to store objects instead of simple values. So taking the above example slightly further you could do something like this:
Class TennisPlayer Public Rank Public Name Public Points End Class Set players = CreateObject("System.Collections.ArrayList") Set player = New TennisPlayer player.Rank = "1" player.Name = "Novak Djokovic" player.Points = "11685" players.Add player Set player = New TennisPlayer player.Rank = "2" player.Name = "Rafael Nadal" player.Points = "7945" players.Add player Set player = New TennisPlayer player.Rank = "3" player.Name = "Roger Federer" player.Points = "6950" players.Add player WScript.Echo Replace("There are {0} players in the draw", "{0}", players.Count) For Each P in players WScript.Echo StrFormat("Player {0}, is ranked {1}, and has {2} ATP points", Array(p.Name, p.Rank, p.Points)) Next Function StrFormat(FS, Args()) Dim i StrFormat=FS For i = 0 To UBound(Args) StrFormat = Replace(StrFormat, "{" & i & "}", args(i)) Next End Function
Which produces the following output:
If you are wondering about the StrFormat function, it is just something I use to make formatting strings easier and you can read about that function here.
I have also written up this same information and method to add items to an array in PowerShell.
Nick Cook says
Thank you! This is very interesting.
Is there a way to make it 2 dimensional?
Paulie says
Wouldn’t the class example provided above give you the dimensional control you need?
Nick Cook says
Paulie, I believe you’re right – I didn’t read down far enough!
Sorry!
Nick Cook says
Ok, me again. How would you sort the class example?
NetAdmin99 says
I am unsure if this is possible and if so how to achieve this.
I want to sub total a column when the chkno changes. I am reading a text file.
Input txt file is below format:
chkno InvTotal
2233 500
2233 600
2250 100
2250 200
2265 700
i want my output text file to look like this
chkno grandtotal
2233 1100
2250 300
2265 700
I am not sure if I need to put this into an array.
I am using vbscript, but if I need to use vb.net, let me know your opinion