?
Solved

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

Posted on 2005-03-16
5
Medium Priority
?
275 Views
Last Modified: 2006-11-17
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
Comment
Question by:Sigh_Man
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:amyhxu
ID: 13562135
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
 
LVL 14

Accepted Solution

by:
amyhxu earned 960 total points
ID: 13562150
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
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13562336
Doesn't .Net have some sort of built-in automatic counter when using For...Next loops?  I thought I had read that somewhere.
0
 
LVL 4

Assisted Solution

by:wyoung76
wyoung76 earned 240 total points
ID: 13562717
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
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13585147
Thanks guys.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question