How do I "Fake" a Bookmark?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a 'for each DataRow in DataTable.Rows loop and I need a way to jump
back to a privious row and continue the loop. I haven't been able to come up
with any clever ways of faking this since Bookmarks don't exist anymore:-(

Thnx,
Grayson
 
Hi Grayson,

You have several options:
- store primary key if there is one and then use DataRowCollection.Find
method
- do a for loop instead and store the index of row
- store a DataRow reference and do a foreach again (this should be the
slowest)
 
Grayson,

To add bookmark functionality, you'd have to override the
IEnumerator.GetEnumerator function and basically implement your own
enumerator. When you'd do it, you'd realize why it was taken out - the
enumerator will now have to deal with remembering it's client's bookmarks -
not a good idea. So basically it's do-able, but it's a pain in the neck and
generally not recommended.

So I'd recommend "faking" it - by using a "For" loop instead of a foreach
loop, and storing the ordinal (index integer) of where you wanted your
bookmark to be, and thence start running the for loop from the bookmark
onwards.

In .NET 2.0, Enumerators are GREATLY simplified using a new keyword called
"yeild", and well - then it'd be much easier to implement bookmarks but you
still run into the problem mentioned in para #1. Essentially it will get
easier to implement though.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Hi Miha,

Thanx for the response.
The Find method returns a DataRow…. So

For Each dr in dt.Rows
If something…
Get the primary key
endif
if something else
dr = dt.Rows.find(primary key)
endif
Next

So if I’m on row 3, get the key = 3
Then I’m on row 10, set dr = 3, I’ll get rows 3, 4, 5, ect again? Sounds too
easy:-)
Sorry I’m not quit far enough along on this VB6 rewrite to be able to test
this just yet, but I will give it a try.


Miha Markic said:
Hi Grayson,

You have several options:
- store primary key if there is one and then use DataRowCollection.Find
method
- do a for loop instead and store the index of row
- store a DataRow reference and do a foreach again (this should be the
slowest)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com

Grayson said:
I have a 'for each DataRow in DataTable.Rows loop and I need a way to jump
back to a privious row and continue the loop. I haven't been able to come
up
with any clever ways of faking this since Bookmarks don't exist anymore:-(

Thnx,
Grayson
 
Thanx Sahil!
Now I got the idea... Sometimes it takes awhile... , Sorry Miha. Use the
index number and I'm right back were I wanted to be and still moving forward.
Perfect
 
Back
Top