Solved

.net find items in structure arraylist

Posted on 2010-09-22
6
698 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

627 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