• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1294
  • Last Modified:

VB.NET 1.1 - efficient way to search in an array List of objects

--------------------------------------------------------------------------------------------------------------------------------
Vb.net 1.1 -Searching an object item inside an array list of objects
--------------------------------------------------------------------------------------------------------------------------------
Is there an efficient and fast way to search an object item inside an array list of objects in .net 1.1? In details&..
I have a class like (See Code Below) -
Public Class MyClass1
    Private MyID As String = ""
    Private MyName As String = ""
    Private MyAddress As String = ""
 
    Public Property ID() As String
        Get
            Return MyID
        End Get
        Set(ByVal Value As String)
            MyID = Value
        End Set
    End Property
 
    Public Property Name() As String
        Get
            Return MyName
        End Get
        Set(ByVal Value As String)
            MyName = Value
        End Set
    End Property
 
    Public Property Address() As String
        Get
            Return MyAddress
        End Get
        Set(ByVal Value As String)
            MyAddress = Value
        End Set
    End Property
End Class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
And I want to search by Name say 'MyFirstName' from an array list of objects of type MYClass1 as -
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Dim arrlMyClass1 As ArrayList		'a large array list of objects of type MyClass1
 Dim objMyObject As New MyClass1
 Dim MyID As String = ""
 
 For Each objMyObject In arrlMyClass1
     If Trim(LCase("MyFirstName")) = Trim(LCase(objMyObject.Name)) Then
        MyID = objMyObject.ID
        Exit For
     End If
 Next
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I am looking for an efficient search method to replace the above to search an object by item Name - say 'MyFirstname'. The For each loop is taking a lot of time if the array list is large.

Open in new window

0
Gurunathans
Asked:
Gurunathans
1 Solution
 
MaxOvrdrv2Commented:
because you declared it as a class, there is no efficient way of doing that...

however, you could always define a function in your class that does it for you.. something like this:
Public Class MyClass1
Public Function Find(ByVal Lst AS ArrayList, ByVal FN AS String) As String
Dim Itm AS ListItem
Dim retval as String
For Each Itm In arrlMyClass1
If Trim(LCase(FN)) = Trim(LCase(Itm.Name)) Then
  retval="Person Found!"
End If
Next
if retval="" then
  return "Not Found!"
else
  return retval
end if
End function

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now