Using the IndexOf method from the List class

E

Ed Cohen

I need to get the index of a list of the following Cities class

Public Class Cities


Private m_cityName As String

Public Sub New()

End Sub


Public Property CityName() As String
Get
Return m_cityName
End Get
Set(ByVal value As String)
m_cityName = value
End Set
End Property
End Class

The examples I have found are too simple. They only use String. Any ideas?
Thank you in advance.

Ed Cohen | (e-mail address removed)
 
T

Thorsten Doerfler

Ed said:
I need to get the index of a list of the following Cities class

Public Class Cities


Private m_cityName As String

Public Sub New()

End Sub


Public Property CityName() As String
Get
Return m_cityName
End Get
Set(ByVal value As String)
m_cityName = value
End Set
End Property
End Class

The examples I have found are too simple. They only use String. Any ideas?

Where is your list?!

Thorsten Doerfler
 
S

SurturZ

Assuming you are looking in a List(Of Cities) for a particular Cities object
that has a particular value of .CityName, you will need to either 1) use a
for...next loop to find the index, 2) Override .Equals() in your Cities class
to use .IndexOf() 3) Implement IEquatable.

Make VB2008's LINQ can also do it, I have no experience with LINQ

I personally recommend the For...Next loop:

Dim lstCities As New List(Of Cities)
'<SNIPPED> fill lstCities with values here
Dim intNewYorkIndex As Integer = -1 '-1 = not found
For i as Integer = 0 To lstCities.Count-1
Dim c as Cities = lstCities(i)
If c.CityName="New York" Then
intNewYorkIndex = i
Exit For
End If
Next i
If intNewYorkIndex = -1 Then MsgBox ("New York not found!")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top