getting row index of dataset

  • Thread starter Thread starter Sam
  • Start date Start date
S

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
 
If you use an indexed loop instead of a For Each loop then you will have the
index at your fingertips.
 
Of course I could include a counter in my loop ... but i was wondering
if there is another way
 
Of course I could include a counter in my loop ... but i was wondering
if there is another way
 
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
 
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


| 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
|
 
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
 
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


| 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
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top