Solved

.net find items in structure arraylist

Posted on 2010-09-22
6
693 Views
Last Modified: 2012-05-10
how can I find the index of an item in an arraylist of structures?

Private Structure _struct
        Public id As Long
        Public title As String
End Structure
Private _structArr As ArrayList

so how do I find an item in the array with an specific id?
in a normal arrayList it's
index = _structArr.IndexOf(id-to-find)

thanks!
0
Comment
Question by:reinoutkool
6 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 33737422
I would use a List(Of _struct) instead of an ArrayList in this case:

Module Module1

    Public Structure _struct

        Public id As Long

        Public title As String

    End Structure

    Sub Main()

        Dim s1, s2, s3 As _struct



        s1.id = 0

        s1.title = "Zero"



        s2.id = 1

        s2.title = "One"



        s3.id = 3

        s3.title = "Three"





        Dim _structArray As New List(Of _struct)

        _structArray.AddRange({s1, s2, s3})



        Dim idToFind = 1

        Dim indexOfItemWithId1 = _structArray.FindIndex(Function(obj As _struct) obj.id = idToFind)



        Console.WriteLine(indexOfItemWithId1.ToString())

        Console.ReadKey()

    End Sub



End Module

Open in new window

0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 33737501
Use a "List(Of _struct)"

Then use LINQ (requires .NET 3.5) to fin the correct item:

dim _structList  as New Lis(of _struct)
' ....
dim item = (From s in _structList where s.Id = 7).Single

0
 

Author Comment

by:reinoutkool
ID: 33738011
What if I have .net 2.0?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 33738060
I was gonna post the LINQ suggestion too, but then noticed you specifically asked to know the index of the item in the array, not to get the item itself. ;)
Anywho...consider upgrading your project to 3.5 (LINQ is pretty handy), or refer to http:#33737422
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 250 total points
ID: 33739893
The only thing you didn't mention is if the ID value is going to be unique... If the ID isn't unique do you require obtaining multiple objects with the same ID? Something to consider...
If the ID is unique you could also use a Dictionary(Of Long, Object) and then search by the key...
0
 

Author Closing Comment

by:reinoutkool
ID: 33857601
partial solution
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

Title # Comments Views Activity
asp.net mvc input file 2 32
Host asp.net pages 5 25
Interview questions for support of a legacy ASP.NET site 4 37
Javascript and Jquery not firing 9 37
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 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

16 Experts available now in Live!

Get 1:1 Help Now