Say I have a list, and I have an object. How do I find the index of that object in the list?
The first position for arrays in VB.NET is zero; same rules apply to any in-built collection/function requiring indexing and to other .
To get the index of an item in a single line, use the FindIndex() and Contains() method. int index = myList. FindIndex(a => a.
To add items to a ListBox, select the ListBox control and get to the properties window, for the properties of this control. Click the ellipses (...) button next to the Items property. This opens the String Collection Editor dialog box, where you can enter the values one at a line.
The list is designed to support the topic Walkthrough: Writing Queries in Visual Basic. It also can be used for any application that requires a list of objects. The code defines the items in the list of students by using object initializers.
You can use FindIndex to find the index of an object in a generic List: This is the most flexible method to get the index of an object.
Dim list As New List(Of Object)
Const myApple = "Apple111"
For i = 0 To 1000
List.Add("Apple" & i)
Next
Dim indexOfMyApple = list.FindIndex(Function(apple) myApple.Equals(apple))
But the IndexOf method is even simplier and more straightforward if you only want to find an object in a List by the DefaultEqualityComparer:
Dim indexOfMyApple = list.IndexOf(myApple)
You can use IndexOf
also if you don't know what type it is, .NET will use Equals to determine if two objects are equal(should be overridden to not only compare references).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With