0

I have a file that shows A song name on the first line, then the genre on the second, and the time on the third.

Example

All You Need is Love-Beatles
Rock
4.25

This goes on. I need some way of corresponding the songs with the genres and then display them in a list box called lstPlayList using Arrays

I have

strSongs(intCount) = objReader.ReadLine()
strGenre(intCount) = objReader.ReadLine()
dblLength(intCount) = Convert.ToDouble(objReader.ReadLine())

For storing every variable but I can't append songs to genres

  • What's your issue? Is there an exception? What are you expecting? – p3tch May 10 '16 at 15:05
  • say the user selects the genre "Rock" from a combo box. My expectation is for the program to find "All You Need is Love-Beatles" and all other rock songs on file and display it to the list box. – demonkiller3418 May 10 '16 at 15:13
  • Dont break up data related to a thing into multiple arrays. [Classes and Lists](http://stackoverflow.com/a/34164458/1070452) keep the data together and makes it easier to manage – Ňɏssa Pøngjǣrdenlarp May 10 '16 at 15:17
  • 1
    put it in a datatable. That would be the easiest to manage – codeMonger123 May 10 '16 at 15:26
  • I agree with putting it into a datatable. You can then use LINQ to easily get all songs belonging to a genre – p3tch May 10 '16 at 15:37

1 Answers1

0

Instead of having three separate arrays for one item, you could indeed as other commenters have said, create a database, but if you don't want to go that far try this.

Create a structure at the top of your Form's class definition list this :-

Structure Song
    Dim Name As String
    Dim Genre As String
    Dim Duration As Single
End Structure

This effectively creates a new Datatype.

Next create a list of this datatype like this :-

Dim Songs As New List(Of Song)

When you read your data from the file, instead of what you've included in your question, you can add the data to the list like this :-

    Dim newSong As Song
    newSong.Name = objReader.ReadLine()
    newSong.Genre = objReader.ReadLine()
    newSong.Duration = Convert.ToDouble(objReader.ReadLine())

Now that you have one list of all the data, you can search the list and return results as a new list of all the results. This is the function that does the searching and returning of a list :-

Private Function SearchSongsByGenre(searchGenre As String) As List(Of Song)
    Dim returnList As New List(Of Song)
    For Each searchSong As Song In Songs
        If searchSong.Genre = searchGenre Then
            returnList.Add(searchSong)
        End If
    Next
    Return returnList
End Function

To use it in another sub, just create a list in the sub called.. say.. SearchResults :-

Dim SearchResults As New List(of Song)
Searchresults = SearchSongsByGenre("Rock")

Now you have a list of all the Songs that have the genre Rock

A lot safer than searching an array and getting data from 3 arrays that could become desynchronised.

David Wilson
  • 4,369
  • 3
  • 18
  • 31