- A Simple Temperature Conversion Program
- Building a Temperature Class
- Putting the Decisions into the Temperature Class
- Using Classes for Format and Value Conversion
- A String Tokenizer Class
- Classes as Objects
- Class Initialization
- Classes and Properties
- Another Interface Example·The Voltmeter
- A vbFile Class
- Programming Style in Visual Basic
Classes as Objects
The primary difference between ordinary procedural programming and object-oriented (OO) programming is the presence of classes. A class is just a module as we have just shown, that has both public and private methods and that can contain data. However, classes are also unique in that there can be any number of instances of a class, each containing different data. We frequently refer to these instances as objects. We'll see some examples of single and multiple instances following.
Suppose we have a file of results from a swimming event stored in a text data file. Such a file might look, in part, like this.
1 Emily Fenn 17 WRAT 4:59.54 2 Kathryn Miller 16 WYW 5:01.35 3 Melissa Sckolnik 17 WYW 5:01.58 4 Sarah Bowman 16 CDEV 5:02.44 5 Caitlin Klick 17 MBM 5:02.59 6 Caitlin Healey 16 MBM 5:03.62
The columns represent place, names, age, club, and time. If we wrote a program to display these swimmers and their times, we'd need to read in and parse this file. For each swimmer, we'd have a first and last name, an age, a club, and a time. An efficient way to keep the data for each swimmer grouped together is to design a Swimmer class and create an instance for each swimmer.
Here is how we read the file and create these instances. As each instance is created, we add it into a Collection object.
Private swimmers As New Collection Private Sub Form_Load() Dim f As Integer, S As String Dim sw As Swimmer Dim i As Integer f = FreeFile 'read in data file and create swimmer instances Open App.Path & "\500free.txt" For Input As #f While Not EOF(f) Line Input #f, S Set sw = New Swimmer 'create instances sw.init S 'load in data swimmers.Add sw 'add to collection Wend Close #f 'put names of swimmers in list box For i = 1 To swimmers.Count Set sw = swimmers(i) lsSwimmers.AddItem sw.getName Next i End Sub
The Swimmer class itself parses each line of data from the file and stores it for retrieval using getXXX accessor functions.
Private frname As String, lname As String Private club As String Private age As Integer Private tms As New Times Private place As Integer '------ Public Sub init(dataline As String) Dim tok As New Tokenizer tok.init dataline 'initilaize string tokenizer place = Val(tok.nextToken) 'get lane number frname = tok.nextToken 'get first name lname = tok.nextToken 'get last name age = Val(tok.nextToken) 'get age club = tok.nextToken 'get club tms.setText tok.nextToken 'get and parse time End Sub '------ Public Function getTime() As String getTime = tms.getFormatted End Function '------ Public Function getName() As String 'combine first and last names and return together getName = frname & " " & lname End Function '------ Public Function getAge() As Integer getAge = age End Function '------ Public Function getClub() As String getClub = club End Function
Each instance of the Swimmer class contains an instance of the Tokenizer that it uses to parse the input string and an instance of the Times class we wrote previously to parse the time and return it in formatted form to the calling program. Having a class contain other classes is a very common ploy in OO programming and is one of the main ways we can build up more complicated programs from rather simple components.
The program that displays these swimmers is shown in Figure 3-6.
Figure 3-6. A list of swimmers and their times, using containment
When you click on any swimmer, her time is shown in the box on the right. The code for showing that time is extremely easy to write, since all the data are in the swimmer class.
Private Sub lsSwimmers_Click() Dim i As Integer Dim sw As Swimmer i = lsSwimmers.ListIndex 'get index of list If i >= 0 Then Set sw = swimmers(i) 'get that swimmer lbTime.Caption = sw.getTime 'display that time End If End Sub