Need help in increasing the size of my array. i have this code but i don't know which of these code does not increase the size of my array even though i increment it.

This is the declarations:

Dim movieArray(0) As String
    Dim ratingArray(0) As String
    Dim i As Integer

This is the code:

movieArray(i) = movieTxt.Text
        ratingArray(i) = ratingCmb.Text
        'System.Array.Sort(movieArray)
        Try
            For Each Str As String In movieArray
                If Str IsNot Nothing Then
                    Dim newItem As New ListViewItem(movieArray(i))
                    newItem.SubItems.Add(ratingArray(i))
                    ListView1.Items.Add(newItem)
                    ratingCmb.SelectedIndex = 0
                    warnNoMovie.Text = ""
                    warnNoRating.Text = ""
                End If
            Next

            ListView1.Items.Clear()
            ReDim Preserve movieArray(i)
            ReDim Preserve ratingArray(i)
            For a = 0 To i
                Dim newItem As New ListViewItem(movieArray(i))
                newItem.SubItems.Add(ratingArray(i))
                ListView1.Items.Add(newItem)
            Next
            i += 1
            MsgBox(movieArray.Length)
        Catch err As Exception
            MsgBox(err.Message)
        End Try

i post the declarations so that if there's wrong with my declarations you can tell me.

Thanks in advance.

See if this helps.

Private ar1(3) As String

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        MsgBox("current Array.Length: " & ar1.Length)
        ReDim Preserve ar1(ar1.Length + 4) '// increase by 5, since Arrays start at 0, not 1 and .Length starts at 1.
        MsgBox("ReDim'ed and New Array.Length: " & ar1.Length)
    End Sub

Hi

If you are in a loop and want to increase the size of an array by one each time you can use the Ubound value like so
Single dimension array:

Dim MyArray(0)
for i =0 to Mylimit
   redim preserve MyArray(ubound(MyArray)+1) 
'Actually Ubound(Array, Dimension) but by default it is one.
   MyArray(i) = Value(i) 
'for purpose of demo value is some sort of collection you wish to store as an array
next

And Multi Dimensional:

dim MyArray(2,0) 
for i =0 to Mylimit
'because we have a multi dimension array we need to specify the dimension (2) being 
'increased - Note you can only change the size of the outer most dimension 
'while using preserve....
   redim preserve MyArray(2, ubound(MyArray, 2)+1)
   MyArray(0,i) = Value1(i)
   MyArray(1,i) = Value2(i)
   MyArray(2,i) = Value3(i)
'for purpose of demo the values are collections you wish to store as an array
next

Thank you both of you for your great help. And i have come up with this code. but there is still problem on it, i was able to add only one element on the array, and after the first array the second array will throw an error.

This is the Code:

Dim i As Integer
        Dim a As Integer
        Try
            If Not IsNothing(movieTxt.Text) Then
                ListView1.Items.Clear()
                If ratingCmb.SelectedItem.Equals("GP") Then
                    movieArray(i) = movieTxt.Text & "," & "GP"
                ElseIf ratingCmb.SelectedItem.Equals("PG-13") Then
                    movieArray(i) = movieTxt.Text & "," & "PG-13"
                ElseIf ratingCmb.SelectedItem.Equals("R") Then
                    movieArray(i) = movieTxt.Text & "," & "R"
                End If
            End If

            Array.Sort(movieArray)
            For i = 0 To a
                Dim movies() = Split(movieArray(i), ",")
                ListView1.Items.Add(movies(i))
                ListView1.Items(i).SubItems.Add(movies(1))
            Next
            a = a + 1
            ReDim Preserve movieArray(a)
            movieTxt.Text = ""

Thanks again.

hi,
So when the user selects something you want to add it to the array and then loop through the array and create a new list?

Okay, you REALLY need to think about your code logically.


What you are doing is this:

1. You have an integer i and an integer a
2. You don’t set any values for i or a
3. You then clear your listview
4. Now you go to point i in moviearray (i=0 because you never gave it a value)( does moviearray exist?)
5. You change the value in moviearray at point i
6. You sort moviearray
7. Now you loop through i =0 to a (which is zero as you never gave it a value)
8. You split out the value of moviearray at i (which will be zero) this makes a new array (movies)
9. You add a new item to listview1 with the value of movies(i) or movies(0)
10. For the listview1.item(i) you add a sub item the value of movies(1)
11. You now increase a to 1 (0+1 =1)
12. You redim movieArray(1)

You are doing this every time you run this routine so you get one list item and one sub list item depending on what you selected.