120 likes | 131 Views
This example demonstrates using classes in Visual Basic 2008 to track members, their music preferences, and manage a music library for an online music sharing service.
E N D
CSI 101 – Elements of Computing Spring 2009 Lecture #15 – Using Classes in Visual Basic 2008 Wednesday, April 22nd
Example Our client has an online music sharing service. They need a means to track their members, their music library, and the favorites of their members.
Okay, let’s work on this • What do we need to do: • Determine objects • Determine properties • Code methods
Objects • Member • Personal data • Contains list of music preferences • Music Library • Series of music • Let’s chart the member data…
Classes Class Music Artist as String Title As String Genre as String Hits as Long
Classes, cont Class Member Name as String Account As Integer Userid as String Password as String Email as String Catalog(5000) As Music
Determine Properties • Class Music • All Member data would be desired • Class Member • Name, Account, and Email • Userid and password should remain hidden • Will have methods to verify or change password • Catalog can’t have a Property • Arrays don’t work
Adding Properties • Our classes are getting big • We’ll work in Wordpad
Next, on to Methods • How will we use these classes? • Music • “Hit” – another member downloads song • Adding a new song is simply creating a new instance • Constructor handles that • Member • Signing in (verify userid and password) • Add song to catalog
Adding Methods • We’ll add methods, and then write a couple of event procedures to use the classes • We’ll work in Wordpad
Using the classes in a Form • Okay, let’s see how to use the Member class • Button on Form to Join: Sub Join_Click( ) DoCmd.OpenForm(“User information”) ‘Opens form to get name, userid, and password – assume stored as data newid = NextID() ‘system function for unique id uid = getPath() ‘system function to get email addr Dim user As New Member(newid, name, userid, pwd, uid) End Sub
Using classes on a Form, cont • Logging in: Sub Login_Click( ) DoCmd.OpenForm(“Get Login”) Dim user As Member user = Find(getPath()) ‘finds user with email addr Dim check As Boolean check = user.Validate(userid, pwd) ‘check against userid and pwd provided on login form If check = False then MsgBox (“Invalid login”) TerminateConnection() End If End Sub