Home > Articles

  • Print
  • + Share This
This chapter is from the book

Using Classes for Format and Value Conversion

It is convenient in many cases to have a method for converting between formats and representations of data. You can use a class to handle and hide the details of such conversions. For example, you might enter an elapsed time in minutes and seconds with or without the colon.

315.20
3:15.20
315.2

Since all styles are likely, you'd like a class to parse the legal possibilities and keep the data in a standard format within. Figure 3-2 shows how the entries “112” and “102.3” are parsed.

03fig02.gifFigure 3-2. A simple parsing program that uses the Times class




The accessor functions for our Times class include the following.

setText (tx as String)
setSingle (t as Single)
getSingle as Single
getFormatted as String
getSeconds as Single

Parsing is quite simple and depends primarily on looking for a colon. If there is no colon, then values greater than 99 are treated as minutes.

Public Function setText(ByVal tx As String) As Boolean
 Dim i As Integer, mins As Long, secs As Single
  errflag = False
  i = InStr(tx, ":")
  If i > 0 Then
  mins = Val(Left$(tx, i - 1))
   secs = Val(Right$(tx, Len(tx) - i))
   If secs > 59.99 Then
     errflag = True
   End If
   t = mins * 100 + secs
 Else
  mins = Val(tx) \ 100
  secs = Val(tx) - (100 * mins)
  If secs > 59.99 Then
    errflag = True
    t = NT
  Else
   setSingle Val(tx)
  End If
 End If
 setText = errflag
End Function

Since illegal time values might also be entered, we test for cases like 89.22 and set an error flag.

Depending on the kind of time measurements these represent, you might also have some non-numeric entries such as NT for no time or in the case of athletic times, SC for scratch or DQ for disqualified. All of these are best managed inside the class. Thus, you never need to know what numeric representations of these values are used internally.

Private Const tmNT As Integer = 10000, tmDQ As Integer = 20000
Private Const tmSCRATCH As Integer = 30000

Some of these are processed in the code represented by Figure 3-3.

03fig03.gifFigure 3-3. The time entry interface, showing the parsing of symbols for No Time, Scratch, and Disqualification




Handling Unreasonable Values

A class is also a good place to encapsulate error handling. For example, it might be that times greater than some threshold value are unlikely and might actually be times that were entered without a decimal point. If large times are unlikely, then a number such as 123473 could be assumed to be 12:34.73.

Public Sub setSingle(tv As Single)
t = tv
If tv > minVal And tv <> tmNT Then
  t = tv / 100
End If
End Sub

The cutoff value minVal may vary with the domain of times being considered and thus should be a variable. While classes do not have a Form_Load event like Forms do, they do have and initialize events where you can set up default values for variables.

Private Sub Class_Initialize()
       minVal = 10000
End Sub

To set up the Initialize event in the IDE, click on the left drop-down in the editor title bar so that Class is selected and select Initialize from the right drop-down as shown in Figure 3-4.

03fig04.gifFigure 3-4. Selecting the Class Initialize method




  • + Share This
  • 🔖 Save To Your Account