Dim myArray(3) 'Re-Declaring a dynamic array
myArray(0) = "UK"
myArray(1)="USA"
myArray(2)="Canada"
myArray(3)="UAE"
Response.Write ("Total element of the array = " & UBound(myArray) )
for i=0 to uBound(myArray)
Response.Write "<br>" & myArray(i)
Next
Here UBound is used to set the maximum looping length to display each element of the array.
In ASP, we can use UBound() and LBound() to find the size of each dimension in a multi-dimensional array:
Dim arr(3, 4) ' A 2D array with 4 rows and 5 columns
Dim total_elements
total_elements = (UBound(arr, 1) - LBound(arr, 1) + 1) * (UBound(arr, 2) - LBound(arr, 2) + 1)
Response.Write("Total elements in the array: " & total_elements)
We can encapsulate the logic to find the array size in a function:
Function ArraySize(arr)
ArraySize = UBound(arr) - LBound(arr) + 1
End Function
Dim arr(10)
Response.Write("Size of the array is: " & ArraySize(arr))
We can loop through an array and display the size along with each element:
Dim arr(5), i
For i = LBound(arr) To UBound(arr)
arr(i) = i * 10 ' Assign values
Next
Response.Write("Size of array: " & UBound(arr) - LBound(arr) + 1 & "<br>")
For i = LBound(arr) To UBound(arr)
Response.Write("Element " & i & " = " & arr(i) & "<br>")
Next
You can dynamically change the size of an array using ReDim:
Dim arr()
ReDim arr(3) ' Set initial size
arr(0) = "A"
arr(1) = "B"
arr(2) = "C"
arr(3) = "D"
Response.Write("Size before resizing: " & UBound(arr) - LBound(arr) + 1 & "<br>")
ReDim Preserve arr(5) ' Resize and preserve existing values
arr(4) = "E"
arr(5) = "F"
Response.Write("Size after resizing: " & UBound(arr) - LBound(arr) + 1 & "<br>")