Results 1 to 5 of 5

Thread: need feedback on this code

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    need feedback on this code

    I am trying to grasp the concept of OOP and I'm making a program that catagorizes my mp3 collection. I created a class and would like to know if I'm on the right track:

    Code:
    Public Class Album
        Private m_artist As String
        Private m_album As String
        Private m_TrackNum As String
        Private m_TrackName As String
    
        Public Property Artist() As String
            Get
                Return m_artist
            End Get
            Set(ByVal Value As String)
                m_artist = Value
            End Set
        End Property
    
        Public Property Album() As String
            Get
                Return m_album
            End Get
            Set(ByVal Value As String)
                m_album = Value
            End Set
        End Property
    
        Public Property TrackNum() As String
            Get
                Return m_TrackNum
            End Get
            Set(ByVal Value As String)
                m_TrackNum = Value
            End Set
        End Property
    
        Public Property TrackName() As String
            Get
                Return m_TrackName
            End Get
            Set(ByVal Value As String)
                m_TrackName = Value
            End Set
        End Property
    End Class
    Last edited by Andy; Feb 11th, 2004 at 09:43 PM.

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    Looks like a good start. You may want to make your TrackNum property an Integer and put some code in to enforce that it is a positive value in the Set routine.

    You also allow the possibility of creating an instance of your object with no data in it. This may be what you want. If not add some constructors something like this:

    VB Code:
    1. Sub New(pArtist As String, pAlbum As String)
    2.     Artist = pArtist ' Could set the member variables directly if you want
    3.     Album = pAlbum
    4. End Sub
    5.  
    6. Sub New(pArtist As String, pAlbum As String, pTrackNum As String, pTrackName As String)
    7.     Artist = pArtist
    8.     Album = pAlbum
    9.     TrackNum = pTrackNum
    10.     TrackName = pTrackName
    11. End Sub
    This world is not my home. I'm just passing through.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    aha...where would I plug in a "constructor"? What exactly is that anyway?

    The reason I made the TrackNum a string, is because it's not going to be used for any calculations and I only need to to represent the "name" of the song. Do you think it would still be a better idea to make it an integer?

  4. #4
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    The Constructor is the New Sub that I've given you code for. It lives in the class itself, normally before the Properties that you've already coded. You can have more than one Constructor as long as the argument lists are different. You use them when you create a new instance of your object (class) as follows:

    VB Code:
    1. Dim Album1 As New Album("The Wall", "Pink Floyd")
    2.     Debug.Writeline(Album1.Artist)
    3.     Debug.Writeline(Album1.Album)

    Visual Studio should show you a tooltip for the different constructors that are available as you type it in.

    I'd still recommend using an Integer for the track number. The point of a class is that you tightly define the data. I would have thought that you want to make sure that there is no chance of the track property being anything other than a positive integer. You probably also should put code in to make sure that it is not possible to set the artist, album or track to an empty string. Something like this:

    VB Code:
    1. Public Property Artist() As String
    2.         Get
    3.             Return m_artist
    4.         End Get
    5.         Set(ByVal Value As String)
    6.             If Value = "" Then
    7.                 Throw New System.Exception("Artist cannot be an empty string")
    8.             Else
    9.                 m_artist = Value
    10.             End If
    11.         End Set
    12.     End Property
    This world is not my home. I'm just passing through.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Wink

    got it! Ya know, every day, it gets a bit easier. But then again.....


    Thanks for the feedback. I hope to get the app working soon like I want it.

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