Problem finding a particular item in a list of objects

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 14912 Views - Last Post: 07 July 2011 - 12:54 PM Rate Topic: -----

#1 JeremyC  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 12-September 10

Problem finding a particular item in a list of objects

Posted 05 July 2011 - 05:33 PM

Hello,

I have a question about locating a particular object in a generic list.

I have 2 lists, one contains a list of every song in a library and the other contains only copies of some the those elements that are added to a playlist.

How would I locate and change a value of an particular item in a list by comparing one of their variables such as name? Or find it's index in the masterlist.
Is This A Good Question/Topic? 0
  • +

Replies To: Problem finding a particular item in a list of objects

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Problem finding a particular item in a list of objects

Posted 05 July 2011 - 05:46 PM

I dont know how you have achieved that, but if you use OO to do your work you can compare instance variable of each one which is distinct using For Each loop
Was This Post Helpful? 0
  • +
  • -

#3 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Re: Problem finding a particular item in a list of objects

Posted 05 July 2011 - 06:38 PM

Have you tried looping through your list?

Can you show us the codes? Tell us the errors.
Was This Post Helpful? 0
  • +
  • -

#4 JeremyC  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 12-September 10

Re: Problem finding a particular item in a list of objects

Posted 06 July 2011 - 05:56 AM

This is the code I ended up with. I didn't really word my question well. I know how to do it this way, but I was curious about whether there is another way. Using Linq for instance to find the instances index value in the list?

My goal was to try and avoid looping through the whole list, but if not that ok too.

            Private Sub RemoveFromPlaylist()
                        Dim intSelectedIndex As Integer = lstPlaylist.SelectedIndex
                        Dim objSelectedSong As Song

                        Try
                                    objSelectedSong = mlstPlaylist.Item(intSelectedIndex)
                                    mlstPlaylist.RemoveAt(intSelectedIndex)
                                    For Each song In mlstSongLibrary
                                                If song.strName = objSelectedSong.strName Then
                                                            song.intInPlayList = 0
                                                End If
                                    Next

                                    lstPlaylist.Items.RemoveAt(intSelectedIndex)
                                    mintCurrentPlaylistLength -= objSelectedSong.intLenghinSeconds
                                    RefreshProgressBar()
                        Catch ex As Exception
                                    ' Stop program from exiting if the user does not have a list item selected.
                        End Try
            End Sub


This post has been edited by JeremyC: 06 July 2011 - 05:58 AM

Was This Post Helpful? 0
  • +
  • -

#5 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 320
  • Joined: 12-January 11

Re: Problem finding a particular item in a list of objects

Posted 06 July 2011 - 07:13 AM

I'm not sure if it's applicable here, but I have recently started using the "contains" method of my lists. (i.e. If myList.Contains(something) then...) Hope it can help. Good luck.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Problem finding a particular item in a list of objects

Posted 06 July 2011 - 08:18 AM

What about IndexOf.

Dim wordToFind as string
wordToFind = "your search text"
dim ndx as Integer = mlstSongLibrary.IndexOf(wordToFind))

If ndx > -1 Then 'something was found
mlstSongLibrary(ndx) = "What do I want to change it to"
End If

Was This Post Helpful? 0
  • +
  • -

#7 JeremyC  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 12-September 10

Re: Problem finding a particular item in a list of objects

Posted 06 July 2011 - 08:31 AM

How would that work if your searching a list of objects with multiple properties though? I get how it would work for a one dimensional search. However each object in my list contains a name, artist, genre, and length.

Is there a way to specify which property I want to use to match and then get it's index number or is the best way to just use a loop?

This post has been edited by JeremyC: 06 July 2011 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem finding a particular item in a list of objects

Posted 06 July 2011 - 08:37 AM

Overload the item property so you can search the list be name and not by index. Example:

Public Class foo : Inherits List(Of String)
  Public Overloads Property Item(ByVal name As String) As String
    Get
      For Each itm As String In Me
        If itm.Equals(name) Then Return itm
      Next
      Return Nothing' or throw error
    End Get
    Set(ByVal value As String)
      value = name
    End Set
  End Property
End Class

This post has been edited by hawkvalley1: 06 July 2011 - 08:38 AM

Was This Post Helpful? 2
  • +
  • -

#9 JeremyC  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 12-September 10

Re: Problem finding a particular item in a list of objects

Posted 07 July 2011 - 06:18 AM

Thank you for the reply, I'm not sure that I fully understand what that does though. The class that I'm using uses the following variables, and a property to allow another object to view the library but not change it. Adding and Removing of songs are handled by by the add and remove procedures.

Option Strict On
Option Explicit On

Public Class SongLibrary
            Private mobjSongLibrary As New List(Of Song)
            Private mstrSongGenreList() As String
            Private mintTotalPlayLength As Integer
            Private mstrTimeString As String

            Public Overridable Sub AddItemToLibrary(ByVal objSong As Song, ByVal blnSort As Boolean)
                        mobjSongLibrary.Add(objSong)
                        If blnSort = True Then
                                    mobjSongLibrary = ReorderList(mobjSongLibrary)
                        End If
                        setCurrentPlayTime()
                        GenerateGenreList()
            End Sub

            Public Sub RemoveItemFromLibrary(ByVal intIndex As Integer)
                        mobjSongLibrary.RemoveAt(intIndex)
                        setCurrentPlayTime()
                        GenerateGenreList()
            End Sub

            ReadOnly Property SongLibrary As List(Of Song)
                        Get
                                    Return mobjSongLibrary
                        End Get
            End Property

End Class
            


So if I understand what your saying I should add something similar to this. Correct?

            ReadOnly Property FindSong(ByVal strNameIn As String) As Song
                        Get
                                    For Each itm In mobjSongLibrary
                                                If itm.strName.Equals(strNameIn) Then
                                                            Return itm
                                                End If
                                    Next
                                    Return Nothing
                        End Get
            End Property



Would it be ok to use a standard for loop as well to find the objects index within the list?
Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem finding a particular item in a list of objects

Posted 07 July 2011 - 06:30 AM

Define your Song as normal. Then define a new class object like I did and name it something that makes sense to you.

Public Class SongList : Inherits List(Of Song)
  Public Overloads Property Item(ByVal name As Song) As Song
    Get
      For Each itm As Song In Me
        If itm.Equals(name) Then Return itm
      Next
      Return Nothing' or throw error
    End Get
    Set(ByVal value As Song)
      value = name
    End Set
  End Property
End Class



Then replace this line:

Private mobjSongLibrary As New List(Of Song)


With,

Private mobjSongLibrary As New SongList

Was This Post Helpful? 1
  • +
  • -

#11 JeremyC  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 12-September 10

Re: Problem finding a particular item in a list of objects

Posted 07 July 2011 - 08:14 AM

OK, so after staring at what you wrote for about 10 min I think I it clicked. OOP is still pretty new to me so let me know if this is what you meant. By inheriting List(Of Song) we add further functionality to, in this case finding a particular song or it's index by name?



Public Class SongList
            Inherits List(Of Song)

      Public ReadOnly Property FindSongByName(ByVal strNameIn As String) As Song
            Get
                  For Each itm As Song In Me
                        If itm.strName.Equals(strNameIn) Then
                              Return itm
                        End If
                  Next
                  Return Nothing
            End Get
      End Property

      Public ReadOnly Property FindSongIndexByName(ByVal strNameIn As String) As Integer
            Get
                  Dim i As Integer
                  For i = 0 To Me.Count - 1
                        If Me.Item(i).strName.Equals(strNameIn) Then
                              Return i
                        End If
                  Next
                  Return Nothing
            End Get
      End Property
End Class


This post has been edited by JeremyC: 07 July 2011 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

#12 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem finding a particular item in a list of objects

Posted 07 July 2011 - 09:18 AM

Yes. And you don't have to use the Item property, but I like the way it looks vs calling a function.

songlist("foo")

songlist.FindSongIndexByName("foo")

Was This Post Helpful? 1
  • +
  • -

#13 JeremyC  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 12-September 10

Re: Problem finding a particular item in a list of objects

Posted 07 July 2011 - 09:45 AM

Cool, thank you very much. For the time being, I think it would be better for me to keep it looking like it's calling a function for my own sanity :).
Was This Post Helpful? 0
  • +
  • -

#14 JeremyC  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 12-September 10

Re: Problem finding a particular item in a list of objects

Posted 07 July 2011 - 10:34 AM

I made the changes but it's caused another issue. I have two functions, one that sorts the list and another that returns a filtered list based on a query.

This is the original function
      Private Function ReorderList(ByVal objListIn As List(Of Song)) As List(Of Song)
            Dim objTempList As New List(Of Song)
            objTempList = (From selection In objListIn Order By selection.strName Select selection).ToList
            Return objListIn
      End Function



I changed the parameters and local vars to SongList objects however the ToList function at the end of LINQ query is causing and exception. I also tried to just keep objTempList as a List(of Song) and CType convert it before returning it but that didn't work either.

I don't understand why it's causing the exception as it's still a list.

Any ideas?
      Private Function ReorderList(ByVal objListIn As SongList) As SongList
            Dim objTempList As New SongList
            objTempList = (From selection In objListIn Order By selection.strName Select selection).ToList
            Return objListIn
      End Function


Was This Post Helpful? 0
  • +
  • -

#15 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem finding a particular item in a list of objects

Posted 07 July 2011 - 10:50 AM

You should be returning the objTempList. Yes I would think it should be the same too. Try leaving anonymous and then cast it.
Private Function ReorderList(ByVal objListIn As SongList) As SongList
      Dim objTempList = (From selection In objListIn Order By selection.strName Select selection).ToList
      Return CType(objTempList, SongList)
End Function

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2