Solved

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

Posted on 2009-05-13
1
1,232 Views
Last Modified: 2013-11-07
--------------------------------------------------------------------------------------------------------------------------------
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
Comment
Question by:Gurunathans
1 Comment
 
LVL 10

Accepted Solution

by:
MaxOvrdrv2 earned 500 total points
ID: 24376139
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now