Results 1 to 8 of 8

Thread: Dim an array to the amount of items in list?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    71

    Dim an array to the amount of items in list?

    Is there a way to dim an array to a value that is the same as a list box item count? I keep trying to do this, but it tells me that I need a constant value. Is there a way I can do this? I want to do this for an order randomizer, but need the program to automatically know the number of choices that need to be randomized.

    VB Code:
    1. Dim Variables (1 to list2.listcount)

  2. #2

  3. #3
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Dim an array to the amount of items in list?

    your quick bull...

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    71

    Re: Dim an array to the amount of items in list?

    I still get a subscript out of range error

  5. #5
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Dim an array to the amount of items in list?

    the first item in a listbox has an index of 0 but the listcount is 1 so to get the count of items in a listbox use,

    VB Code:
    1. List2.ListCount - 1

    so if there are no entries in the listbox then saying,

    VB Code:
    1. ReDim MyArray(List2.ListCount - 1) As Integer

    is saying,

    VB Code:
    1. ReDim MyArray(- 1) As Integer

    which you cant do.

    so you need to add items to the listbox or set a condition like this,


    VB Code:
    1. Dim MyArray() As Integer 'or whatever type you need
    2.    
    3.     List2.AddItem "vb"
    4.     List2.AddItem "forums"
    5.    
    6.     ' you need to add something otherwise you'll be redimming to -1
    7.     ' if you havent adding anything to listbox then
    8.    
    9.     If Not List2.ListCount = 0 Then
    10.         ReDim MyArray(List2.ListCount - 1) As Integer
    11.     End If

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Dim an array to the amount of items in list?

    Yes, you need to check if list is populated:
    VB Code:
    1. Dim MyArray() As Integer 'or whatever type you need
    2.  
    3. If List2.ListCount > o Then
    4.     Redim MyArray(List2.ListCount - 1)
    5. Else
    6.     Redim MyArray(0)
    7. End If

  7. #7
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Dim an array to the amount of items in list?

    Quote Originally Posted by RhinoBull
    Yes, you need to check if list is populated:
    VB Code:
    1. If List2.ListCount > o Then
    I always wondered when they first designed the keyboard, why did they put "o" right below "0"... hmm...

    Anyways, that is supposed to be:
    VB Code:
    1. If List2.ListCount > [b]0[/b] Then

  8. #8

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