By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,588 Members | 1,920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,588 IT Pros & Developers. It's quick & easy.

getting row index of dataset

P: n/a
Sam
Hi,
Here is my code :

For Each row As DataRow In ds.Tables(0).Rows
next

How can I get the index of the row being processed ?

Thx

Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Sam
Of course I could include a counter in my loop ... but i was wondering
if there is another way

Nov 21 '05 #2

P: n/a
If you use an indexed loop instead of a For Each loop then you will have the
index at your fingertips.
"Sam" <sa**************@voila.fr> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,
Here is my code :

For Each row As DataRow In ds.Tables(0).Rows
next

How can I get the index of the row being processed ?

Thx

Nov 21 '05 #3

P: n/a
Sam
Of course I could include a counter in my loop ... but i was wondering
if there is another way

Nov 21 '05 #4

P: n/a
Sam
sorry I was writing my message while you were posting yours. I wasn't
meant to be rude :)
So ok, i'll use a counter instead
thx

Nov 21 '05 #5

P: n/a
> Of course I could include a counter in my loop ... but i was wondering
if there is another way


Why?

Cor
Nov 21 '05 #6

P: n/a
Sam,
In addition to using an indexed For loop. You can simply increment a counter
with a For Each.

For index As Integer = 0 to ds.Tables(0).Rows.Count - 1
Dim row As DataRow = ds.Tables(0).Rows(index)
...
Next

or

Dim index As Integer = 0
For Each row As DataRow In ds.Tables(0).Rows
...
index += 1
Next

Either works, performance may vary based on the specific collection. I
normally use the second as it seems cleaner.

Hope this helps
Jay
"Sam" <sa**************@voila.fr> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
| Hi,
| Here is my code :
|
| For Each row As DataRow In ds.Tables(0).Rows
| next
|
| How can I get the index of the row being processed ?
|
| Thx
|
Nov 21 '05 #7

P: n/a
Jay,

In addition to using an indexed For loop. You can simply increment a
counter
with a For Each.

For index As Integer = 0 to ds.Tables(0).Rows.Count - 1
Dim row As DataRow = ds.Tables(0).Rows(index)
...
Next

or

Dim index As Integer = 0
For Each row As DataRow In ds.Tables(0).Rows
...
index += 1
Next

Either works, performance may vary based on the specific collection. I
normally use the second as it seems cleaner.

So you see how personal preference can be different, for me is that with the
first.

(Maybe because I am with the second never sure that there is not used a kind
of dictionary index, what does in the first case not matter, I hope this
describes what I want to say with that. I don't believe that that is done
here by the way)

That leads me to use consequently to use the first when I need an index. Can
be a crazy feeling by the way.

Cor
Nov 21 '05 #8

P: n/a
Cor,
| (Maybe because I am with the second never sure that there is not used a
kind
| of dictionary index, what does in the first case not matter, I hope this
| describes what I want to say with that. I don't believe that is done
| here by the way)
If the first does not have an "index" to use performance can greatly matter!
For example if the "list" is based on a linked list, the performance of the
first can be horrific as you need to recount each element each time you call
the Item (indexer) property...

Also as you suggest, For index will not work with a hashtable (dictionary).
Why have an "oddball solution" and use For index for some loops & For each
for others. Hence I normally use For each for all loops...

Just a thought
Jay
"Cor Ligthert" <no************@planet.nl> wrote in message
news:u3**************@TK2MSFTNGP14.phx.gbl...
| Jay,
|
|
| > In addition to using an indexed For loop. You can simply increment a
| > counter
| > with a For Each.
| >
| > For index As Integer = 0 to ds.Tables(0).Rows.Count - 1
| > Dim row As DataRow = ds.Tables(0).Rows(index)
| > ...
| > Next
| >
| > or
| >
| > Dim index As Integer = 0
| > For Each row As DataRow In ds.Tables(0).Rows
| > ...
| > index += 1
| > Next
| >
| > Either works, performance may vary based on the specific collection. I
| > normally use the second as it seems cleaner.
| >
| So you see how personal preference can be different, for me is that with
the
| first.
|
| (Maybe because I am with the second never sure that there is not used a
kind
| of dictionary index, what does in the first case not matter, I hope this
| describes what I want to say with that. I don't believe that that is done
| here by the way)
|
| That leads me to use consequently to use the first when I need an index.
Can
| be a crazy feeling by the way.
|
| Cor
|
|
Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.