Nested Dyanmic Arrays...I Think
I'm creating a simple media player. I'm having difficulty creating nested arrays of classes.
I would like to have "Band(0).Album(0).Song(0)" (corresponding to first song of first album of first band).
I currently have (stripped version):
Code:
Class Song
Public Title as String
Public Path as String
End Class
I have a sub to "for each" of the bands, "for each" album of each band and "for each" song in each album of each band. Now I'm having trouble grouping all this up into a nested array. So I can call each song using the method above. Can someone give me pointers?
Re: Nested Dyanmic Arrays...I Think
You don't want arrays. You want collections. You need to define a class for each level and then each class will have a property that exposes a collection of the class for the level below, e.g.
vb.net Code:
Public Class Song
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
Public Class Album
Private _name As String
Private _songs As New List(Of Song)
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public ReadOnly Property Songs() As List(Of Song)
Get
Return _songs
End Get
End Property
End Class
Public Class Artist
Private _name As String
Private _albums As New List(Of Album)
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public ReadOnly Property Albums() As List(Of Album)
Get
Return _albums
End Get
End Property
End Class
Re: Nested Dyanmic Arrays...I Think
Sorry for my ignorance on this one, but I'm not sure how to implement this. Here's my looping "search" routine:
Code:
Sub MusicList()
Dim MusicDirectory As New DirectoryInfo("d:\Music")
Dim Bands As DirectoryInfo() = MusicDirectory.GetDirectories("*.*")
Dim Band As DirectoryInfo
For Each Band In Bands 'loop though folder containing Band Folders
Dim BandDirectory As New DirectoryInfo(Band.FullName)
Dim Albums As DirectoryInfo() = BandDirectory.GetDirectories("*.*")
Dim Album As DirectoryInfo
For Each Album In Albums 'Loop though Band Folder containing each album
Dim AlbumDirectory As New DirectoryInfo(Album.FullName)
Dim Songs As FileInfo() = AlbumDirectory.GetFiles("*.mp3")
Dim song As FileInfo
For Each song In Songs 'Loop through Album Folder containing Songs
Next
Next
Next
End Sub
I'm thinking it should create a new collection of "Songs" then add that to the current Album. Then once a collection of "Albums" has been made, add that to the collection for the current Band. Then once the Bands collection is made, add that to the Music Collection. But I just cant figure out how to nest the collection into the parent collection. Then be able to pull it out by "Music.Band(0).Album(0).Song(0).Title"
Re: Nested Dyanmic Arrays...I Think
I think I got it....Thank-you for your help!
Re: Nested Dyanmic Arrays...I Think
When you create a new Album it already contains a collection of Songs. When you create a Song you add it to that collection. The same goes for Bands and Albums.
vb.net Code:
Dim bands As New List(Of Band)
Dim band As Band
Dim album As Album
Dim song As Song
For Each bandFolder In Directory.GetDirectories("D:\Music")
band = New Band With {.Name = bandFolder}
bands.Add(band)
For Each albumFolder In Directory.GetDirectories(bandFolder)
album = New Album With {.Name = albumFolder}
band.Albums.Add(album)
For Each songFile In Directory.GetFiles(albumFolder, "*.mp3")
song = New Song With {.Name = Path.GetFileNameWithoutExtension(songFile)}
album.Songs.Add(song)
Next
Next
Next