School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,035 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,260 people online right now. Registration is fast and FREE... Join Now!




Collections, IEnumerable & IEnumerator (Part 1)

 
Reply to this topicStart new topic

> Collections, IEnumerable & IEnumerator (Part 1)

AdamSpeight2008
Group Icon



post 20 Oct, 2009 - 01:42 PM
Post #1


Collections, IEnumerable & IEnumerator (Part 1)
This multi part tutorial is all Collections, IEnumerable and IEnumerator.
I'll cover IEnumerable & IEnumerator in Part 2, as what they are will take a little explaining.

Setup
A Form with a listbox and button.
CODE

Dim a As String() = {"a", "b", "c", "d", "e", "f", "g"}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Me.ListBox1.Items.AddRange(a)
End Sub


Removing Items from a collection.

Let do it using the Item
CODE

Private Sub But_Remove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Remove.Click
  For Each si In Me.ListBox1.SelectedItems
   Me.ListBox1.Items.Remove(si)
  Next
End Sub

Attached Image
QUOTE("Error Message")
List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.


Let do it using the Indices
CODE

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  For i As Int32 = 0 To Me.ListBox1.SelectedIndices.Count - 1
   Me.ListBox1.Items.RemoveAt(Me.ListBox1.SelectedIndices(i))
  Next

End Sub

Attached Image

QUOTE("Error Message")
Index was outside the bounds of the array.


A Solution but it's Backwards
CODE

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  For i As Int32 = Me.ListBox1.SelectedIndices.Count - 1 To 0 Step -1
   Me.ListBox1.Items.RemoveAt(Me.ListBox1.SelectedIndices(i))
  Next
End Sub


Why do I receive the above to errors with the first two but not in the third?
To know why, you first have to understands what happens when you remove you remove an item.
The simple answer is your change the size of the array/collection. (More information on the first error in part 2)
Note: the code block is to preserve the whitespace
CODE

[ A ][ B ][ C ][ D ][ E ][ F ]

Suppose we remove want [ A ] and [ B ] or Indices {0,1}
CODE

[ B ][ C ][ D ][ E ][ F ]

What is the indice of element [ B ]? Is it same as before?
Answer: 0. No.

So lets see why do it in reverse works.
CODE

[ A ][ B ][ C ][ D ][ E ][ F ]

Suppose we remove want [ A ] and [ B ] or Indices {0,1}, remember we're doing this in reverse so [ B ][ A ] or {1,0}
CODE

[ A ][ B ][ C ][ D ][ E ][ F ]

So removing [ B ] we're left with
CODE

[ A ][ C ][ D ][ E ][ F ]

What is the indice of element [ A ]? Is it same as before?
Answer: 0. Yes.

The reason it works is simple the next index is always going to be within the bounds of the now resized (smaller) collection.

Coming up in Part 2: IEnumerable and IEnumerator
Edit: Added in link to Part 2
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 08:43AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month