: For Each always takes the next record in each iteration of the loop.
: This presents a problem if the table is modified during the
: iteration process. For example: Suppose in your table, the 3rd and
: 4th records are bound to be deleted. Once the For Each loop reaches
: the 3rd record, it is deleted. The 4th record becomes the 3rd, and
: 5th record becomes the 4th. Then it reads the next record (4th new
: count; 5th old count), thus stepping over one of the record, which
: needed to be deleted.
: The problem becomes even more pronounced if someone sorts the
: records, while a For Each loop is iterating it. For example: suppose
: you have a lot of records holding expenses. You use a For Each loop
: to sum those expenses. What if someone sorts those simultaneously to
: find the highest expense. This would lead to a sum, which is useless.
: To protect from these (and other) potential problems, most (if not
: all) For Each loop implementations will fail immediately if a
: modification in the data is detected, which is not instantiated by
: its own iterator.
Well here's the kicker, I *have* code that works doing this (in another project), here it is:
For Each dr As DataRow In data.Tables(0).Rows
If dr(0).ToString().ToLower() = "name"
dr.Delete()
ElseIf dr(0).ToString().ToLower() = "image"
dr.Delete()
ElseIf dr(1).ToString().ToLower() = "not applicable"
dr.Delete()
End If
Next
' Display author information
tblAuthorDetails.DataSource = data.Tables(0)
tblAuthorDetails.DataBind()
and that works perfectly. So why would this code work and not the code above? Doesn't make sense, does it?
<%
'// Programmed By: Zantos
'// VisualProgramming.NET
'// http://vp.funurl.com/
'// visualprogramming@gmail.com
%>