Get last (most recently added) record from a list
Get last (most recently added) record from a list
(OP)
Hi Folks
I have an app I wrote last year as an exercise in C#. In it I used a List object to accumulate a group of records that I eventually printed out on a form.
Now I am revisiting it and I would like to add an Undo feature. But to make this work I need to know what the information was in the last record added. It's important because I need to learn what was in the destination field before the action (a drag and drop operation). I'd like to query the list and get the ID # (stored in the record) or the index of the last records added so I can access to the fields to read them, then use the index (or ID # to delete the record from the list. But I'm having a heckuva time trying to figure out how to do this.
Is there anyone that can drop a little sample gode on how to do this? My searches of the online resources aren't giving me the answers I need.
Thanks
Craig (Amesville)
I have an app I wrote last year as an exercise in C#. In it I used a List object to accumulate a group of records that I eventually printed out on a form.
Now I am revisiting it and I would like to add an Undo feature. But to make this work I need to know what the information was in the last record added. It's important because I need to learn what was in the destination field before the action (a drag and drop operation). I'd like to query the list and get the ID # (stored in the record) or the index of the last records added so I can access to the fields to read them, then use the index (or ID # to delete the record from the list. But I'm having a heckuva time trying to figure out how to do this.
Is there anyone that can drop a little sample gode on how to do this? My searches of the online resources aren't giving me the answers I need.
Thanks
Craig (Amesville)
RE: Get last (most recently added) record from a list
[Code=c#]
List<string> myList = new List<string>();
for (int i = 0; i < 20; i++)
myList.Add(string.Format("Line {0}", i+1));
string lastLine = myList[myList.Count()-1];
[/code]
The result is that lastLine contains "Line 20"
If you look in the docs, their is an indexer listed in the properties of List. The C# docs are quite fantastic; if you search the Msdn you can typically find anything you need.
RE: Get last (most recently added) record from a list
RE: Get last (most recently added) record from a list
RE: Get last (most recently added) record from a list
So wherever you process drag&drop operations or whatever else changes the list, also add the operations done on the list in eg another list, kept in chronological order. You abviously need to store old values (if they changed) old items. In the simplest case you could copy the whole list, in it's current state, to have a snapshot you can use as undo step, but that'll be very memory intensive with large lists, obviously.
Bye, Olaf.
RE: Get last (most recently added) record from a list