Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get last (most recently added) record from a list

Status
Not open for further replies.

Amesville

Programmer
Oct 10, 2011
93
US
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)
 
Is this not working for you or something...?

Code:
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];

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.
 
And this is why I should stop replying using my android phone lol
 
You won't only want to remove the laat list item (by index). For a general undo feature (which is not at all very basic) I'd add something similar to a transaction log (I'm a database guy).

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.
 
Thanks, I will try this out. For some reason I was having trouble hitting on the proper syntax to make this work and the examples I was looking at were not helpful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top