- 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
Building a Temperature Class
As we noted in the previous chapter, a class in VB is a module that can contain both public and private functions and subroutines and can hold data values as well. It is logically the same as a Form, except that it has no visual aspects to it. These functions and subroutines in a class are frequently referred to collectively as methods.
Class modules are also like Basic Types or C structs that allow you to keep a set of data values in a single named place and fetch those values using get and set functions, which we then refer to as accessor methods.
You create a class module from the VB integrated development environment (IDE) using the menu item Project | Add class module. Then, you select the Properties window (using function key F4) and enter the module's name. In this example, we'll call the class module clsTemp.
What we want to do is to move all of the computation and conversion between temperature scales into this new clsTemp class module. One way to design this module is to rewrite the calling programs that will use the class module first. In the code sample below, we create an instance of the clsTemp class and use it to do whatever conversions are needed.
Private Sub btConvert_Click() Dim enterTemp As Single, newTemp As Single Dim clTemp As New clsTemp 'create class instance If opFahr.Value Then clTemp.setCels txTemperature lbNewtemp.Caption = Str$(clTemp.getFahr) Else clTemp.setFahr txTemperature lbNewtemp.Caption = Str$(clTemp.getCels) End If
Note that to create a working copy of a class (called an instance) you have to use the new keyword with the Dim statement.
Dim clTemp As New clsTemp 'create class instance
If you simply declare a variable without the New keyword,
Dim clTemp as clsTemp
you have created a pointer to a class instance but have not initialized an actual instance until you actually create one using New. You can set the value of the pointer you created using the Set keyword.
Set clTemp = New clsTemp 'create instance of clsTemp
In this program, we have two set methods—setCels and setFahr—and two get methods—getCels and getFahr.
These methods put values into the class and retrieve other values from the class. The actual class is just this.
Private temperature As Single Public Sub setFahr(tx As String) temperature = 5 * (Val(tx) - 32) / 9 End Sub Public Sub setCels(tx As String) temperature = Val(tx) End Sub Public Function getFahr() As Single getFahr = 9 * (temperature / 5) + 32 End Function Public Function getCels() As Single getCels = temperature End Function
Note that the temperature variable is declared as private, so it cannot be “seen” or accessed from outside the class. You can only put data into the class and get it back out using the four accessor methods. The main point to this code rearrangement is that the outer calling program does not have to know how the data are stored and how they are retrieved: that is only known inside the class. In this class we always store data in Celsius form and convert on the way in and out as needed. We could also do validity checks for legal strings on the way in, but since the Val function returns zeros and no error for illegal strings, we don't have to in this case.
The other important feature of the class is that it actually holds data. You can put data into it, and it will return it at any later time. This class only holds the one temperature value, but classes can contain quite complex sets of data values.
We could easily modify this class to get temperature values out in other scales without still ever requiring that the user of the class know anything about how the data are stored or how the conversions are performed.
Converting to Kelvin
Absolute zero on the Celsius scale is defined as –273.16° degrees. This is the coldest possible temperature, since it is the point at which all molecular motion stops. We can add a function
Public Function getKelvin() As Single getKelvin = temperature + 273.16 End Function
without any changes to the visual client at all. What would the setKelvin method look like?