Solved

.net find items in structure arraylist

Posted on 2010-09-22
6
697 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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 figure out error 3 47
.Net remove carriage returns, line feeds and tabbs 5 37
Data Analysis 7 59
sql connection error null reference exception 9 29
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…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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