mbg_is_doing_VB
September 4th, 2009, 02:07 PM
Maybe someone here can shed some light.
I created a Windows Mobile application with a ListView control. I want to add items to this ListView.
Public Class Form1
Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
Me.Close()
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim i As Short
For i = 1 To 5
Dim listViewItem As New ListViewItem("ItemSuccess" + i.ToString)
listViewItem.SubItems.Add("SubItemSuccess" + i.ToString)
ListView1.Items.Add(listViewItem)
'listViewItem = Nothing
Next
End Sub
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
Dim i As Short
Dim listViewItem As New ListViewItem()
listViewItem.SubItems.Add("")
For i = 1 To 5
listViewItem.SubItems.Item(0).Text = "ItemFail" + i.ToString
listViewItem.SubItems.Item(1).Text = "SubItemFail" + i.ToString
ListView1.Items.Add(listViewItem)
Next
End Sub
End Class
While sub Menu2 succeeds, every run a new ListViewItem instance is created. What if the counter doesn't run to 5, but to say, 50,000?
Every new instance of the ListViewItem class generates a new instance in memory, not to mention the amount of time it will take. Even if I set the instance of ListViewItem to Nothing at the end of each run, I still lose precious cpu cycles.
So, I wrote sub Menu3. Here I create the instance outside of the loop and reuse it every time. Would be great if it worked, but it doesn't. Actually on the second run (i.e. i=2), ListView1.Items.Add(listViewItem) throws an exception, "Value does not fall within the expected range.". During debugging I found out, that the property ListView1.Index is set to "1", whereas during the first run it is set to "-1".
Does anyone have a solution to make the sub Menu3 work?
I created a Windows Mobile application with a ListView control. I want to add items to this ListView.
Public Class Form1
Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
Me.Close()
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim i As Short
For i = 1 To 5
Dim listViewItem As New ListViewItem("ItemSuccess" + i.ToString)
listViewItem.SubItems.Add("SubItemSuccess" + i.ToString)
ListView1.Items.Add(listViewItem)
'listViewItem = Nothing
Next
End Sub
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
Dim i As Short
Dim listViewItem As New ListViewItem()
listViewItem.SubItems.Add("")
For i = 1 To 5
listViewItem.SubItems.Item(0).Text = "ItemFail" + i.ToString
listViewItem.SubItems.Item(1).Text = "SubItemFail" + i.ToString
ListView1.Items.Add(listViewItem)
Next
End Sub
End Class
While sub Menu2 succeeds, every run a new ListViewItem instance is created. What if the counter doesn't run to 5, but to say, 50,000?
Every new instance of the ListViewItem class generates a new instance in memory, not to mention the amount of time it will take. Even if I set the instance of ListViewItem to Nothing at the end of each run, I still lose precious cpu cycles.
So, I wrote sub Menu3. Here I create the instance outside of the loop and reuse it every time. Would be great if it worked, but it doesn't. Actually on the second run (i.e. i=2), ListView1.Items.Add(listViewItem) throws an exception, "Value does not fall within the expected range.". During debugging I found out, that the property ListView1.Index is set to "1", whereas during the first run it is set to "-1".
Does anyone have a solution to make the sub Menu3 work?