March
March

Reputation: 635

dim ASP Array

I want to define an array.

My question is Dim x(999) or Dim x(9999) will cost the same or Dim x(9999) will waste more resource?

Thank you very much!!

Upvotes: 1

Views: 3694

Answers (4)

AnthonyWJones
AnthonyWJones

Reputation: 189457

Arrays are allocated in contiguous memory. Hence an array of 10000 elements will occupy 10 times the memory of one that needs 1000 elements.

You can start off small and grow your array as the need arises. I've used this sort of class in the past to create variable length list type.

Class List
    Dim maItems
    Dim mlCount

    Public Sub Class_Initialize()
       ReDim maItems(8)
       mlCount = 0
    End Sub

    Public Function Add(Item)
       If mlCount = UBound(maItems) ReDim Preserve maItems(mlCount * 2)
       mlCount = mlCount + 1
       maItems(mlCount) = Item
       Add = mlCount
    End Function

   Public Property Get Item(Index)
       If Index < 1 Or Index > mlCount Then Error.Raise 9, "List", "Subscript out of Range"
       Item = maItems(Index)
   End Property

   Public Property Get Count()
       Count = mlCount
   End Property
End Class    

Upvotes: 1

PropellerHead
PropellerHead

Reputation: 929

I added a little to Anthony W Jones's code, so it returns an Array (ToArray()) with the correct size with only the relevant elements.

Class List    
    Dim maItems    
    Dim mlCount

    Public Sub Class_Initialize()       
        ReDim maItems(8)
        mlCount = 0    
    End Sub    

    Public Function Add(Item)            
        If mlCount = UBound(maItems) Then ReDim Preserve maItems(mlCount * 2)       
        maItems(mlCount) = Item  
        mlCount = mlCount + 1
        Add = mlCount   
    End Function

    Public Property Get ToArray()

        ReDim Result(Params.Count-1)

        Dim i
        For i = 0 to Params.Count-1
            Result(i) = Params.maItems(i)
        Next

        ToArray = Result        
    End Property

    Public Property Get Item(Index)       
        If Index < 1 Or Index > mlCount Then Error.Raise 9, "List", "Subscript out of Range"       
        Item = maItems(Index)   
    End Property   

    Public Property Get Count()       
        Count = mlCount   
    End Property   
End Class

Upvotes: 0

balexandre
balexandre

Reputation: 75073

Is it that big of an array?

why don't you assign it to be Dynamically and then expand as it grows?

<%
Dim myDynArray() 

ReDim myDynArray(1)
myDynArray(0) = "Albert Einstein"
myDynArray(1) = "Mother Teresa"

ReDim Preserve myDynArray(3)
myDynArray(2) = "Bill Gates"
myDynArray(3) = "Martin Luther King Jr."

For Each item In myDynArray
    Response.Write(item & "<br />")
Next
%>

output of the code above is

Albert Einstein Mother Teresa Bill Gates Martin Luther King Jr. 

Upvotes: 0

1800 INFORMATION
1800 INFORMATION

Reputation: 135285

Naturally creating an array with 9999 elements in it will use more memory than an array with only 999. I suspect that this is not really your question though. Maybe you are trying to figure out a way to allocate enough memory for a dynamic amount of data? You can resize arrays in classic ASP (VBScript) using the ReDim statement - you could do this once the true size is known.

Upvotes: 2

Related Questions