Asked by:
get last item in an list in vb.net

Question
-
User52515197 posted
This command returns the last object from the list without any condition for the search
My code:
aList.Last()
example :
Structure group Public name Public Sub New(value As String) name = value End Sub End Structure Dim listGroups As New List(Of group) listGroups.Add(New group("group 1")) Dim groupName As string = listGroups.Last().name
Thursday, October 11, 2018 4:41 PM
All replies
-
User2053451246 posted
What is your question?
Thursday, October 11, 2018 4:53 PM -
User52515197 posted
How to get the last value in the list ?
This was a recurring question, but because the expiration date was over, I had to re-raise it.
Thursday, October 11, 2018 6:57 PM -
User1120430333 posted
Dim groupName As string = listGroups(listGroups.count-1).name
You are using the listGroup.count that is the total number of items in the list - 1, because that list is a 0 based index (total count - 1). You are also using the index to address an item in the list by its index (listItem(idx).property).
Thursday, October 11, 2018 7:35 PM -
User-893317190 posted
This command returns the last object from the list without any condition for the searchFrom the sentence above, it seems that you want to get the last value with a specify condition.
If so, the method Last() also accept a Function as its parameter, you could specify the condition through the Function using a bool expression.
Below is my code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim listGroups As New List(Of group) listGroups.Add(New group("group 1")) listGroups.Add(New group("group 2")) listGroups.Add(New group("group 3")) listGroups.Add(New group("group 4")) listGroups.Add(New group("group 5")) Response.Write(listGroups.Last(Function(m) m.name = "group 4").name) End Sub Structure group Public name Public Sub New(value As String) name = value End Sub End Structure
The result.
For more information about use Function as a parameter , you could refer to
Best regards,
Ackerly Xu
Friday, October 12, 2018 7:21 AM -
User52515197 posted
Thank you for your guidance
But how can I find it when I'm not aware of the content of the last item on the listFriday, October 12, 2018 2:56 PM -
User1120430333 posted
zokaee hamid
Thank you for your guidance
But how can I find it when I'm not aware of the content of the last item on the listList.Count is the total number of items in the list. List.Count-1 is going to be the last item in the list. Do the math and the last object in the list can be accessed by its index in the list.
dim name = list(idx number).name
dim name = list(0).name 'gets the name property of the first object in the list.
list(1).name = 'zokaee hamid' sets the property of the second object in the list
All you are trying to do is get access to last object in the list. Does it need to be complicated by not using the object's index number in the list?
Friday, October 12, 2018 3:55 PM -
User52515197 posted
If the list is empty, the negative value will be entered
Saturday, October 13, 2018 8:08 PM -
User1120430333 posted
zokaee hamid
If the list is empty, the negative value will be entered
If there are no items in the list, the list.count will equal 0. So you have to check the list.count before you try using an indexer in order to address an item in the list by its index in the list.
If List.Count() > 0 then
'' address an item in the list by its index
endif
Saturday, October 13, 2018 8:42 PM