• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

How do I get the current DataRow in a For Each ...Next loop

my code says something like:

For Each x as DataRow in dsInterest.Tables(0).Rows
' Do something to the previous DataRow
Next

...without doing a Dim i as integer 1 To 10 and then applying the For Next based on DataRow(i)

Thanks in advance.
0
Sigh_Man
Asked:
Sigh_Man
  • 2
  • 2
2 Solutions
 
amyhxuCommented:
dim PreviousRow as datarow = dsInterest.Tables(0).Rows(0)
dim CurrentRow as datarow = dsInterest.Tables(0).Rows(0)

For Each x as DataRow in dsInterest.Tables(0).Rows
    PreviousRow = CurrentRow
    CurrentRow = x
    ' Do something to the PreviousRow
Next
0
 
amyhxuCommented:
This may be better:

dim PreviousRow as datarow = Nothing
dim CurrentRow as datarow = Nothing

For Each x as DataRow in dsInterest.Tables(0).Rows
    PreviousRow = CurrentRow
    CurrentRow = x
    If Not PreviousRow Is Nothing Then
        ' Do something to the PreviousRow
    End If
Next
0
 
Sigh_ManAuthor Commented:
Doesn't .Net have some sort of built-in automatic counter when using For...Next loops?  I thought I had read that somewhere.
0
 
wyoung76Commented:
Using the For Each construct simply uses the enumertor returned by the collection/array/whatever.  The basic members supported by any enumertor are only .Current, .MoveNext, and .Reset.  Basically, it's only a read-forward operation.

If you want to access any particular item in the collection, then you'd more probably want to use an indexed For...Next loop like the one you're avoiding, otherwise use amyhxu's suggested solution to keep track of the previous Row.

Also, just if you haven't guessed it, .Rows.Count will give you the number of Rows in your Table :o)
0
 
Sigh_ManAuthor Commented:
Thanks guys.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now