Testing the COven ClassNow you need to write a shell program that can be used to test your new class. Figure 16.3 shows a form that can be used to test the COven class. Figure 16.3. A form for testing the COven class.
There are two buttons on the form. The btnUpdate button is used to update the state of the COven object, and the btnExit button is used to end the program. The txtOvenTemp text box is a little different from other text boxes you have used so far in this book. In this case, you have set the ReadOnly property to True instead its default value of False . Therefore, the user cannot enter anything into the text box. There are a number of radio button objects on the form. The names of the radio buttons should make it easy for you to decipher their use in the program. For example, the Bake, Broil, Clean, and Off radio buttons are named rbOvenOn , rbBroil , rbClean , and rbOvenOff . Equally obvious names are used for the other radio buttons. The names of the buttons, along with the code in the following section, make it clear what the purpose of each button is. The Code for Testing the COven ClassThe code for testing the COven class is somewhat long, but it's not too complicated. The code is presented in Listing 16.3. Listing 16.3 Code for Testing the COven ClassOption Explicit On Option Strict On Public Class frmOvenTest Inherits System.Windows.Forms.Form Private Const BROILTEMP As Integer = 550 ' Broiler temperature Private Const CLEANTEMP As Integer = 600 ' Self-clean temperature Dim MyOven As New COven() Private Sub btnUpdate_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnUpdate.Click ' Purpose: This button acts upon the state of the oven data as set ' by the radio buttons and the text boxes. If CheckSettings() = 0 Then ' See if everything in proper state Exit Sub End If If rbOvenOff.Checked = True Then txtTemp.Text = "0" End If UpdateOvenTemperature() txtTemp.Text = txtOvenTemp.Text End Sub Private Sub UpdateOvenTemperature() ' Purpose: This routine compares the current oven temperature to ' the desired temperature and updates it. To add a little ' realism to the process, the change is delayed by using ' the VBN DateDiff() function. ' ' Argument list: ' None ' ' Return value: ' N/A Dim Start, Finish As Double Dim NewDate As Date Dim StartTemp, EndTemp, DiffTemp, Increments, Offset As Integer StartTemp = CInt(txtOvenTemp.Text) EndTemp = CInt(txtTemp.Text) Increments = 5 Offset = 5 DiffTemp = EndTemp - StartTemp If DiffTemp < 0 Then ' If we are decreasing the temp DiffTemp = 0 - DiffTemp ' Make it positive Increments = -5 ' Count down rather than up End If While DiffTemp > 0 NewDate = DateAdd(DateInterval.Second, 1, Now) ' Delay about a second While DateDiff(DateInterval.Second, Now, NewDate) > 0 ' An empty loop End While StartTemp += Increments DiffTemp -= Offset txtOvenTemp.Text = CStr(StartTemp) Me.Update() ' Update the form End While End Sub Private Function CheckSettings() As Integer ' Purpose: This subroutine simply checks to see if all ' the radio buttons are consistent with ' the action being done ' ' Argument list: ' N/A ' ' Return value: ' integer True if OK, False otherwise ' Is the oven on? If rbOvenOff.Checked = True Then txtTemp.Text = "0" txtOvenTemp.Text = "0" MessageBox.Show("The oven is off") Return 0 End If ' Broiling usually has oven door open If rbDoorClosed.Checked = True And rbBroil.Checked = True Then MessageBox.Show("Probably should open the oven door") If txtTemp.Text = "0" Then txtTemp.Text = CStr(BROILTEMP) End If Return 0 End If ' If we are baking or self-cleaning, close the door If rbDoorClosed.Checked = False And rbBroil.Checked <> True Then If rbClean.Checked = False Then Beep() MessageBox.Show("Things bake faster with oven door closed") Return 0 End If End If ' Show the temperature If rbDoorClosed.Checked = False And rbBroil.Checked = True Then txtTemp.Text = CStr(BROILTEMP) Return -1 End If ' If we are cleaning the oven... If rbDoorClosed.Checked = False And rbClean.Checked = True Then Beep() MessageBox.Show("Close door during self-cleaning") Return 0 End If ' ...set the temperature to the max. If rbDoorClosed.Checked = True And rbClean.Checked = True Then txtTemp.Text = CStr(CLEANTEMP) Return -1 End If Return -1 End Function Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load InitializeOven(MyOven) End Sub Private Sub InitializeOven(ByVal MyOven As COven) ' Purpose: This sets up the starting state of the oven as shown by ' the text boxes. ' ' Argument list: ' MyOven a COven object. This does not really need to be ' passed in since it has module scope. However, ' doing it this way would allow us to change its ' scope and still be able to use this procedure ' without changing this code. ' ' Return value: ' N/A If MyOven.OvenStatus = 0 Then ' Show oven status rbOvenOff.Checked = True Else rbOvenOn.Checked = True End If If MyOven.DoorStatus = 0 Then ' Show door status rbDoorClosed.Checked = True Else rbDoorOpen.Checked = True End If If MyOven.DoorStatus = 0 Then ' Show door status rbDoorClosed.Checked = True Else rbDoorOpen.Checked = True End If If MyOven.LightStatus = 0 Then ' Oven light rbLightOff.Checked = True Else rbLightOn.Checked = True End If txtTemp.Text = CStr(MyOven.Temperature) ' Temperature txtOvenTemp.Text = CStr(MyOven.Temperature) End Sub Private Sub btnExit_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnExit.Click Me.Dispose() End Sub ' ================= The radio buttons ========================= Private Sub rbOvenOn_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles rbOvenOn.Leave rbOvenOn.Checked = True MyOven.OvenStatus = -1 End Sub Private Sub rbOvenOff_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles rbOvenOff.Leave rbOvenOff.Checked = True MyOven.OvenStatus = 0 End Sub Private Sub rbLightOn_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles rbLightOn.Leave rbLightOn.Checked = True MyOven.LightStatus = 0 End Sub Private Sub rbLightOff_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles rbLightOff.Leave rbLightOff.Checked = True MyOven.LightStatus = 0 End Sub Private Sub rbDoorOpen_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles rbDoorOpen.Leave rbDoorOpen.Checked = True MyOven.DoorStatus = -1 End Sub Private Sub rbDoorClosed_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles rbDoorClosed.Leave rbDoorClosed.Checked = True MyOven.DoorStatus = 0 End Sub Private Sub rbBroil_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles rbBroil.Leave If rbDoorClosed.Checked = True And rbBroil.Checked = True Then MessageBox.Show("Probably should open the oven door") Exit Sub End If MyOven.Broil() txtTemp.Text = CStr(BROILTEMP) End Sub Private Sub rbClean_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles rbClean.Leave MyOven.SelfClean() txtTemp.Text = CStr(CLEANTEMP) End Sub ' ============= the text boxes ======================== Private Sub txtTemp_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles txtTemp.Leave If txtTemp.Text = "0" Then MyOven.Temperature = 0 rbOvenOff.Checked = True End If End Sub End Class The code in Listing 16.3 begins by defining two symbolic constants for the broiling and self-cleaning temperatures . Next it defines a COven class object named MyOven . Because the MyOven definition occurs outside any procedure, it has module scope. If this were a live program, it would be a good idea to place the definition of MyOven inside a procedure to hide it from the rest of the program. All the action focuses on what happens when the user clicks the Update button. In the btnUpdate object's Click event, the code calls CheckSettings() to see if the radio buttons are in a state that allows the user to operate the oven. For example, if the rbOvenOff button is selected, the oven cannot do anything. If the rbBake button is selected but the oven door is open, an error message is issued. (Trying to bake something with the oven door open slows down the process a bunch.) You can read through the code and see what other states are examined. The following code fragment is used to inject a small delay each time the txtOvenTemp text box is updated: While DiffTemp > 0 NewDate = DateAdd(DateInterval.Second, 1, Now) ' Delay about a second While DateDiff(DateInterval.Second, Now, NewDate) > 0 ' An empty loop End While StartTemp += Increments DiffTemp -= Offset txtOvenTemp.Text = CStr(StartTemp) Me.Update() ' Update the form End While The DateAdd() function sets the date interval to 1 second, and the second parameter to the call creates a 1-second difference between the current date (as held in Now ). The return value is assigned to NewDate , which is 1 second in the future. The While loop simply wastes time until the difference between NewDate and Now becomes . The remainder of the code updates the displayed oven temperature and updates txtOvenTemp on the form. The code in Listing 16.3 actually updates things in less than 1 second, probably because of the time lag between the two calls to DateAdd() and DateDiff() . You can experiment with the second parameter in DateAdd() to see the impact it has on the delay. |