Counter for "foreach"

  • Thread starter Thread starter Maya
  • Start date Start date
M

Maya

Hi,

How do I find the number of the current row in a DataRow being looped
in a foraech statement without manually creating an increment-by-one
counter?

Thanks,

Maya
 
I am pretty sure there is no built-in way to do that, the only way is to
declare an Integer (or if you prefer some of numeric type that uses
integers) and increment it with each loop. I have never seen anybody use any
technique other than this, so I would just continue to use it since it is
not complicated and it involves very few extra steps.
 
Maya,

AFAIK is there a new part in 2.0 that can handles your question. However,
it comes not in my mind directly where it is on MSDN. Because that I will
not use it, do I not search for it. Maybe somebody else knows it.

Therefore why not use the For Index loop if you want to have the index of a
row, that is made for it?

I hope this gives an idea,

Cor
 
Cor Ligthert said:
Maya,

AFAIK is there a new part in 2.0 that can handles your question.

No, AFAIK there isn't. It would be nice to have it, though.
 
Actually, in 2.0 there IS a new method on the Rows collection that can
return the row ordinal. It's coded like this:
For Each myRow As DataRow in myDataTable.Rows
intRowOrdinal = myDataTable.Rows.IndexOf(myRow)
...
Next

I can understand why you haven't found it. One of the more serious problems
with Visual Studio 2005 is not any malfunction, it's the fact that many of
the new features are so poorly documented that they have not been
discovered. We have asked MS to address this issue but so far we have not
seen any substantive change in the doc. I and other authors are working as
hard as we can to get RTM-based documentation in your hands that highlights
these new (and often important) new features without marketing fluff or
echoing the doc.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Back
Top