Hello all. I have a question on searching a List(Of CLass). I am not sure how it is labeled List<OF T> or List(Of Class) However you label it here is the Class EXT
Public Class EXT
Implements IComparable
Private m_extension As String
Private m_exe As String
Private m_description As String
'Public Sub New()
'End Sub
Public Sub New(ByVal eExtension As String, _
ByVal eDescription As String, ByVal eExecutable As String)
m_extension = eExtension
m_description = eDescription
m_exe = eExecutable
End Sub
Public Property EXTENSION() As String
Get
Return m_extension.ToLower
End Get
Set(ByVal value As String)
m_extension = value
End Set
End Property
Public Property DESCRIPTION() As String
Get
Return m_description
End Get
Set(ByVal value As String)
m_description = value
End Set
End Property
Public Property EXECUTABLE() As String
Get
Return m_exe
End Get
Set(ByVal value As String)
m_exe = value
End Set
End Property
Public Function ToArray() As String()
Dim ret(2) As String
ret(0) = m_extension
ret(1) = m_description
ret(2) = m_exe
Return ret
End Function
Public Function CompareTo(ByVal obj As Object) As Integer _
Implements System.IComparable.CompareTo
Dim other As EXT = DirectCast(obj, EXT)
Return Me.EXTENSION.CompareTo(other.EXTENSION)
End Function
End Class
Here is a sample the CSV file there are over 936 records.
EXTENSION,DESCRIPTION,EXECUTABLE
.flac,Music,C:\Program Files\JetAudio\jetaudio.exe
.flv,Flash Video file,C:\Program Files\Combined Community Codec Pack\MPC\mpc-hc.exe
.....
I have lstExt which is a List Of EXT. I fill it from a CSV file and display it on a DataGridView through a BindingSource. I have a textbox which the user types in the extension they want to search for and presses the Button. The searched for extension if found is selected and scrolled to in the DataGridView. Now here are the multiple ways in which to search for my extension.
Private Sub SearchForExtension(ByVal sItem As String)
If sItem.Length > 2 Then
'Get Selected Index of user entered file extension
'LINQ way
Dim query = From exts In lstExt _
Where exts.EXTENSION = sItem _
Select exts
Dim idx1 As Integer = lstExt.IndexOf(CType(query(0), EXT))
'Way (2)
Dim foundCustomer As EXT = Nothing
foundCustomer = lstExt.FirstOrDefault(Function(s) s.EXTENSION = sItem)
Dim idx As Integer = lstExt.IndexOf(foundCustomer)
'Way(3)
Dim sIndex As Integer = lstExt.FindIndex(Function(p As EXT) p.EXTENSION = sItem)
'Way (4)
Dim myLocatedObject As EXT = lstExt.Find(Function(p As EXT) p.EXTENSION = sItem)
Dim myIndex As Integer = lstExt.IndexOf(myLocatedObject)
If idx > -1 Then
dgv_csv.ClearSelection()
dgv_csv.Rows(idx).Selected = True
dgv_csv.FirstDisplayedScrollingRowIndex = idx
dgv_csv.PerformLayout()
End If
End If
End Sub
As you can see from the SearchForExtension Sub I have 4 different ways to search for the extension. The index is used to select the searched for row in the DataGridView. My question is which is best/preferred?
Thanks