|
-
Jan 27th, 2004, 08:43 AM
#1
Thread Starter
Frenzied Member
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.
-
Jan 27th, 2004, 09:41 AM
#2
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:
Sub New(pArtist As String, pAlbum As String)
Artist = pArtist ' Could set the member variables directly if you want
Album = pAlbum
End Sub
Sub New(pArtist As String, pAlbum As String, pTrackNum As String, pTrackName As String)
Artist = pArtist
Album = pAlbum
TrackNum = pTrackNum
TrackName = pTrackName
End Sub
This world is not my home. I'm just passing through.
-
Jan 27th, 2004, 11:34 PM
#3
Thread Starter
Frenzied Member
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?
-
Jan 28th, 2004, 03:14 AM
#4
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:
Dim Album1 As New Album("The Wall", "Pink Floyd")
Debug.Writeline(Album1.Artist)
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:
Public Property Artist() As String
Get
Return m_artist
End Get
Set(ByVal Value As String)
If Value = "" Then
Throw New System.Exception("Artist cannot be an empty string")
Else
m_artist = Value
End If
End Set
End Property
This world is not my home. I'm just passing through.
-
Jan 28th, 2004, 11:28 PM
#5
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|