tags:

views:

729

answers:

4
+1  Q: 

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

+2  A: 

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.

1800 INFORMATION
+1  A: 

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 myArray(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
AnthonyWJones
I think the above code should instead be:Dim maItems Dim mlCount Public Sub Class_Initialize() ReDim maItems(8) mlCount = 0 End Sub
PropellerHead
@PropellerHead: Well spotted. I spend most of my time in C# these days. ;)
AnthonyWJones
A: 

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.
balexandre
You need to be careful not to extend the array too often, preservation frequently requires the array as a whole to be copied in memory.
AnthonyWJones
I agree, but was only for the example sake.
balexandre
A: 

Hi all,

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
PropellerHead