469,312 Members | 2,493 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

getting row index of dataset

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
8 9995
Sam
Of course I could include a counter in my loop ... but i was wondering
if there is another way

Nov 21 '05 #2
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
Sam
Of course I could include a counter in my loop ... but i was wondering
if there is another way

Nov 21 '05 #4
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
> 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
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
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
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.

Similar topics

1 post views Thread by Dave Bailey | last post: by
2 posts views Thread by Justin | last post: by
1 post views Thread by Diffident | last post: by
1 post views Thread by Michel Moreno | last post: by
4 posts views Thread by darrel | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.