Solved

.net find items in structure arraylist

Posted on 2010-09-22
6
692 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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