Results 1 to 5 of 5

Thread: Nested Dyanmic Arrays...I Think

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Public Class Song
    2.  
    3.     Private _name As String
    4.  
    5.     Public Property Name() As String
    6.         Get
    7.             Return _name
    8.         End Get
    9.         Set(ByVal value As String)
    10.             _name = value
    11.         End Set
    12.     End Property
    13.  
    14. End Class
    15.  
    16.  
    17. Public Class Album
    18.  
    19.     Private _name As String
    20.     Private _songs As New List(Of Song)
    21.  
    22.     Public Property Name() As String
    23.         Get
    24.             Return _name
    25.         End Get
    26.         Set(ByVal value As String)
    27.             _name = value
    28.         End Set
    29.     End Property
    30.  
    31.     Public ReadOnly Property Songs() As List(Of Song)
    32.         Get
    33.             Return _songs
    34.         End Get
    35.     End Property
    36.  
    37. End Class
    38.  
    39.  
    40. Public Class Artist
    41.  
    42.     Private _name As String
    43.     Private _albums As New List(Of Album)
    44.  
    45.     Public Property Name() As String
    46.         Get
    47.             Return _name
    48.         End Get
    49.         Set(ByVal value As String)
    50.             _name = value
    51.         End Set
    52.     End Property
    53.  
    54.     Public ReadOnly Property Albums() As List(Of Album)
    55.         Get
    56.             Return _albums
    57.         End Get
    58.     End Property
    59.  
    60. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    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"

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Nested Dyanmic Arrays...I Think

    I think I got it....Thank-you for your help!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Dim bands As New List(Of Band)
    2. Dim band As Band
    3. Dim album As Album
    4. Dim song As Song
    5.  
    6. For Each bandFolder In Directory.GetDirectories("D:\Music")
    7.     band = New Band With {.Name = bandFolder}
    8.     bands.Add(band)
    9.  
    10.     For Each albumFolder In Directory.GetDirectories(bandFolder)
    11.         album = New Album With {.Name = albumFolder}
    12.         band.Albums.Add(album)
    13.  
    14.         For Each songFile In Directory.GetFiles(albumFolder, "*.mp3")
    15.             song = New Song With {.Name = Path.GetFileNameWithoutExtension(songFile)}
    16.             album.Songs.Add(song)
    17.         Next
    18.     Next
    19. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width