Solved

.net find items in structure arraylist

Posted on 2010-09-22
6
694 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB 2005 Tooltips on Form Load Event 15 29
Modal Popup Extender control 1 19
Sort GridView by ID Descending 1 10
Complex SQL statement in VB.NET 3 5
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

776 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