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:
Dim Variables (1 to list2.listcount)
Re: Dim an array to the amount of items in list?
You'd have to use dynamic array for that:
VB Code:
Dim MyArray() As Integer 'or whatever type you need
Redim MyArray(List2.ListCount - 1)
Re: Dim an array to the amount of items in list?
your quick bull... :rolleyes:
Re: Dim an array to the amount of items in list?
I still get a subscript out of range error :(
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,
so if there are no entries in the listbox then saying,
VB Code:
ReDim MyArray(List2.ListCount - 1) As Integer
is saying,
VB Code:
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:
Dim MyArray() As Integer 'or whatever type you need
List2.AddItem "vb"
List2.AddItem "forums"
' you need to add something otherwise you'll be redimming to -1
' if you havent adding anything to listbox then
If Not List2.ListCount = 0 Then
ReDim MyArray(List2.ListCount - 1) As Integer
End If
Re: Dim an array to the amount of items in list?
Yes, you need to check if list is populated:
VB Code:
Dim MyArray() As Integer 'or whatever type you need
If List2.ListCount > o Then
Redim MyArray(List2.ListCount - 1)
Else
Redim MyArray(0)
End If
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:
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:
If List2.ListCount > [b]0[/b] Then
Re: Dim an array to the amount of items in list?
Lol... but both look like a bagel... :)