Results 1 to 5 of 5

Thread: array help

  1. #1

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551

    array help

    VB Code:
    1. Dim varPIEQTY()
    2. Dim varpiei As Integer
    3.  
    4. For varpiei = 0 To 100
    5.     varPIEQTY(varpiei) = 0
    6. Next varpiei

    subscript out of range

    on
    "varPIEQTY(varpiei) = 0"

    ideas?

    i haven't worked too much with arrays, but im trying to assign the array varPIEQTY(0) all the way to varPIEQTY(100) all = 0

    this because i'll be filling in randoms parts in the array, and it seems if i haven't declared varPIEQTY(40) = 0 and i don't have a varPIEQTY(0), i get an errror...

  2. #2
    You never allocated the space for the array:
    VB Code:
    1. Dim varPIEQTY(0 To 10)
    2.  
    3. ' or
    4.  
    5. Dim varPIEQTY()
    6. ReDim varPIEQTY(0 To 10)

  3. #3
    Addicted Member Rikk's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta
    Posts
    131
    you have to 'resize' the array before adding to it... I added a line to bump up the array size every 'varpiei' loop...

    Dim varPIEQTY()
    Dim varpiei As Integer

    For varpiei = 0 To 100
    ReDim Preserve varPIEQTY(varpiei)
    varPIEQTY(varpiei) = 0
    Next varpiei
    Rikk =\=
    Starcraft, Protoss Scout Driver!

  4. #4

    Thread Starter
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    ahh.. ic...

    thanks!

  5. #5
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Definitely dont do Rikk's suggestion... follow the turtle
    Redim preserve is unnecessary in this situation and will slow down the array allocation considerably.

    VB Code:
    1. 'In forms declaration area (ie top of form)
    2. Dim varPIEQTY()
    3. Dim varpiei As Integer
    4.  
    5. 'Within the particular event
    6. ReDim Preserve varPIEQTY(100) 'Outside of the loop
    7. For varpiei = 0 To 100
    8.       varPIEQTY(varpiei) = 0
    9. Next varpiei

    PS If u only want to set all elements to zero (or "" for a string) u can simple use the Erase command which is much faster than a loop

    VB Code:
    1. Erase varPIEQTY
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width