locked
class of class instance RRS feed

  • Question

  • Good day!

    How to create a class with strings array?

    For example, I need a class with artist names and artist albums and album songs. Somthing like this:

    MyCollection.A_name = "Singer1"
    MyCollection.A_album(1) = "Disc1"
    MyCollection.A_album(1).A_song(1) = "Song1"
    MyCollection.A_album(1).A_song(2) = "Song2"
    MyCollection.A_album(2) = "Disc2"
    MyCollection.A_album(2).A_song(1) = "Song1"
    MyCollection.A_album(2).A_song(2) = "Song2"

    etc.

    Is it possible? And if so, than how to describe this kind of class?

    Thanks


    Aleksey

    Saturday, December 15, 2012 11:38 AM

Answers

  • Hi, like this?

    Sub Main() Dim myCollection As New MyCollection() myCollection.Name = "Singer1" myCollection.Albums(0) = New Album("Disc1") myCollection.Albums(0).Songs(0) = "Song1" myCollection.Albums(0).Songs(1) = "Song2" myCollection.Albums(1) = New Album("Disc2") myCollection.Albums(1).Songs(0) = "Song1" myCollection.Albums(1).Songs(1) = "Song2" End Sub
    Class MyCollection Public Sub New() Albums = New Album(1) {} Name = "" End Sub 'public string[string[]] Albums{get;set;} Public Property Albums() As Album() Get Return m_Albums End Get Set(value As Album()) m_Albums = Value End Set End Property Private m_Albums As Album() Public Property Name() As String Get Return m_Name End Get Set(value As String) m_Name = Value End Set End Property Private m_Name As String End Class Class Album Public Sub New(disk__1 As String) Songs = New String(1) {} Disk = disk__1 End Sub Public Property Songs() As String() Get Return m_Songs End Get Set(value As String()) m_Songs = Value End Set End Property Private m_Songs As String() Public Property Disk() As String Get Return m_Disk End Get Set(value As String) m_Disk = Value End Set End Property Private m_Disk As String End Class

    or with List(T) ?
        Sub Main()
            Dim myCollection As New MyCollection()
            myCollection.Name = "Singer1"
            myCollection.Albums.Add(New Album("Disc1"))
            myCollection.Albums(0).Songs.Add("Song1")
            myCollection.Albums(0).Songs.Add("Song2")
            myCollection.Albums.Add(New Album("Disc2"))
            myCollection.Albums(1).Songs.Add("Song1")
            myCollection.Albums(1).Songs.Add("Song2")
        End Sub
    
        Class MyCollection
            Public Sub New()
                Albums = New List(Of Album)()
                Name = ""
            End Sub        
            Public Property Albums() As List(Of Album)
                Get
                    Return m_Albums
                End Get
                Set(value As List(Of Album))
                    m_Albums = Value
                End Set
            End Property
            Private m_Albums As List(Of Album)
            Public Property Name() As String
                Get
                    Return m_Name
                End Get
                Set(value As String)
                    m_Name = Value
                End Set
            End Property
            Private m_Name As String
        End Class
    
        Class Album
            Public Sub New(disk__1 As String)
                Songs = New List(Of String)()
                Disk = disk__1
            End Sub
            Public Property Songs() As List(Of String)
                Get
                    Return m_Songs
                End Get
                Set(value As List(Of String))
                    m_Songs = Value
                End Set
            End Property
            Private m_Songs As List(Of String)
            Public Property Disk() As String
                Get
                    Return m_Disk
                End Get
                Set(value As String)
                    m_Disk = Value
                End Set
            End Property
            Private m_Disk As String
        End Class


    Koopakiller [kuːpakɪllɐ] http://koopakiller.ko.ohost.de/

    • Marked as answer by Newfriend Sunday, December 16, 2012 4:59 PM
    Saturday, December 15, 2012 12:00 PM
  • You can get the count of songs in an album with
    myCollection.Albums(0).Songs.Count
    Another way is implement IList to get a Count property and all methods of a standart List(T) and manage all self.
    Public ReadOnly Property Count() As Integer
    	Get
    		Return Albums.Count
    	End Get
    End Property



    Koopakiller [kuːpakɪllɐ] http://koopakiller.ko.ohost.de/

    • Marked as answer by Newfriend Sunday, December 16, 2012 4:59 PM
    Saturday, December 15, 2012 2:08 PM

All replies

  • Hi, like this?

    Sub Main() Dim myCollection As New MyCollection() myCollection.Name = "Singer1" myCollection.Albums(0) = New Album("Disc1") myCollection.Albums(0).Songs(0) = "Song1" myCollection.Albums(0).Songs(1) = "Song2" myCollection.Albums(1) = New Album("Disc2") myCollection.Albums(1).Songs(0) = "Song1" myCollection.Albums(1).Songs(1) = "Song2" End Sub
    Class MyCollection Public Sub New() Albums = New Album(1) {} Name = "" End Sub 'public string[string[]] Albums{get;set;} Public Property Albums() As Album() Get Return m_Albums End Get Set(value As Album()) m_Albums = Value End Set End Property Private m_Albums As Album() Public Property Name() As String Get Return m_Name End Get Set(value As String) m_Name = Value End Set End Property Private m_Name As String End Class Class Album Public Sub New(disk__1 As String) Songs = New String(1) {} Disk = disk__1 End Sub Public Property Songs() As String() Get Return m_Songs End Get Set(value As String()) m_Songs = Value End Set End Property Private m_Songs As String() Public Property Disk() As String Get Return m_Disk End Get Set(value As String) m_Disk = Value End Set End Property Private m_Disk As String End Class

    or with List(T) ?
        Sub Main()
            Dim myCollection As New MyCollection()
            myCollection.Name = "Singer1"
            myCollection.Albums.Add(New Album("Disc1"))
            myCollection.Albums(0).Songs.Add("Song1")
            myCollection.Albums(0).Songs.Add("Song2")
            myCollection.Albums.Add(New Album("Disc2"))
            myCollection.Albums(1).Songs.Add("Song1")
            myCollection.Albums(1).Songs.Add("Song2")
        End Sub
    
        Class MyCollection
            Public Sub New()
                Albums = New List(Of Album)()
                Name = ""
            End Sub        
            Public Property Albums() As List(Of Album)
                Get
                    Return m_Albums
                End Get
                Set(value As List(Of Album))
                    m_Albums = Value
                End Set
            End Property
            Private m_Albums As List(Of Album)
            Public Property Name() As String
                Get
                    Return m_Name
                End Get
                Set(value As String)
                    m_Name = Value
                End Set
            End Property
            Private m_Name As String
        End Class
    
        Class Album
            Public Sub New(disk__1 As String)
                Songs = New List(Of String)()
                Disk = disk__1
            End Sub
            Public Property Songs() As List(Of String)
                Get
                    Return m_Songs
                End Get
                Set(value As List(Of String))
                    m_Songs = Value
                End Set
            End Property
            Private m_Songs As List(Of String)
            Public Property Disk() As String
                Get
                    Return m_Disk
                End Get
                Set(value As String)
                    m_Disk = Value
                End Set
            End Property
            Private m_Disk As String
        End Class


    Koopakiller [kuːpakɪllɐ] http://koopakiller.ko.ohost.de/

    • Marked as answer by Newfriend Sunday, December 16, 2012 4:59 PM
    Saturday, December 15, 2012 12:00 PM
  • Yes, It works. 1-st option is good for me. And the last question. You do have New sub which is adding new instances. Is it possible to count them somhow? Like bellow:

            Dim myCollection As New MyCollection()
            myCollection.Name = "Singer1"
            myCollection.Albums.Add(New Album("Disc1"))
            myCollection.Albums(0).Songs.Add("Song1")
            myCollection.Albums(0).Songs.Add("Song2")

    msgbox (myCollection.Albums(0).Songs.COUNT)

    If I have a "count" instance of this class, how to update it each time I add new album to particular singer?


    Aleksey

    Saturday, December 15, 2012 12:40 PM
  • You can get the count of songs in an album with
    myCollection.Albums(0).Songs.Count
    Another way is implement IList to get a Count property and all methods of a standart List(T) and manage all self.
    Public ReadOnly Property Count() As Integer
    	Get
    		Return Albums.Count
    	End Get
    End Property



    Koopakiller [kuːpakɪllɐ] http://koopakiller.ko.ohost.de/

    • Marked as answer by Newfriend Sunday, December 16, 2012 4:59 PM
    Saturday, December 15, 2012 2:08 PM