A Sample Program Using the Arithmetic Operators


A good way to explore the arithmetic operators is to write a simple program that lets you enter values for the operands and operators and see what the result is.

To create this program, you need to start a new project and call it MathOperators . Then, add controls to the form as shown in Figure 9.1.

Figure 9.1. Placement of controls for the arithmetic operators program.

graphics/09fig01.jpg

The layout of the objects on the form is similar to that of a Visual Basic .NET assignment statement. As you learned earlier, a simple assignment statement in Visual Basic .NET has this form:

  ArithmeticResult  =  Operand1 Operator Operand2  

In Figure 9.1, the objects are arranged such that the statement looks like this:

 txtResult = txtOperand1 Operator txtOperand2 

The user supplies the two operands by typing them into the appropriate text boxes and clicking the operator he or she wants to use. The user then clicks the Calculate button, and the operation is performed.

The complete code for this program is presented in Listing 9.1.

Listing 9.1 The Arithmetic Operators Program
 Public Class frmArithmeticOperators  Inherits System.Windows.Forms.Form  Dim WhichOne As Integer  Windows Form Designer generated code  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _      System.EventArgs) Handles MyBase.Load   RadioButton1.Checked = True  End Sub  Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged   WhichOne = 1  End Sub  Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged   WhichOne = 2  End Sub  Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged   WhichOne = 3  End Sub  Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged   WhichOne = 4  End Sub  Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged   WhichOne = 5  End Sub  Private Sub RadioButton6_CheckedChanged(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles RadioButton6.CheckedChanged   WhichOne = 6  End Sub  Private Sub RadioButton7_CheckedChanged(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles RadioButton7.CheckedChanged   WhichOne = 7  End Sub  Private Sub RadioButton8_CheckedChanged(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles RadioButton8.CheckedChanged   txtOperand2.Text = ""   WhichOne = 8  End Sub  Private Sub RadioButton9_CheckedChanged(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles RadioButton9.CheckedChanged   txtOperand2.Text = ""   WhichOne = 9  End Sub  Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As _               System.EventArgs) Handles btnCalc.Click   Select Case WhichOne    Case 1     txtResult.Text = CStr(CDbl(txtOperand1.Text) + _                CDbl(txtOperand2.Text))    Case 2     txtResult.Text = CStr(CDbl(txtOperand1.Text) - _                CDbl(txtOperand2.Text))    Case 3     txtResult.Text = CStr(CDbl(txtOperand1.Text) * _                CDbl(txtOperand2.Text))    Case 4     txtResult.Text = CStr(CDbl(txtOperand1.Text) / _                CDbl(txtOperand2.Text))    Case 5     txtResult.Text = CStr(CInt(txtOperand1.Text) \ _                CInt(txtOperand2.Text))    Case 6     txtResult.Text = CStr(CDbl(txtOperand1.Text) ^ _                CDbl(txtOperand2.Text))    Case 7     txtResult.Text = CStr(CInt(txtOperand1.Text) Mod _                CInt(txtOperand2.Text))    Case 8     txtResult.Text = CStr(+(CDbl(txtOperand1.Text)))    Case 9     txtResult.Text = CStr(-(CDbl(txtOperand1.Text)))   End Select   WhichOne = 1  End Sub  Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As _               System.EventArgs) Handles btnExit.Click   Me.Dispose()  End Sub End Class 

There appears to be a lot of code in Listing 9.1, but much of it is duplicate code. Note at the top of the program the integer variable WhichOne . Because it is defined outside any procedure, WhichOne has module scope and is available to every procedure.

Each of the operators is associated with a radio button. Because all the operator radio buttons are grouped within a group box, clicking on one radio button automatically deselects whatever button was previously clicked. There are nine radio buttons , and their code is contextually identical. Consider the code for the first radio button:

 Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged  WhichOne = 1 End Sub 

In this code, you can see that when RadioButton1 is clicked, WhichOne is assigned the value 1 . If you look at the code for all the other eight buttons, you see that the only change in most of them is the value that is assigned to WhichOne .

Note that radio buttons 8 and 9 set the text box for the second operand to be empty. You have them do this because these two operators are unary operators and do not require a second operand.

All the work is done in the Select Case statement in the btnCalc object's Click() event. You will not learn about the Select Case statement until Chapter 11, "Making Decisions," but you should get an idea of what it does from the code in Listing 9.1. Given a value for WhichOne , the Select Case statement executes the code for the case that corresponds to WhichOne .

You can also see in Listing 9.1 that the first radio button is selected by default when the program starts. If there is code that you want to have executed the moment the program begins execution, you can place it in the Form1_Load() event. This statement:

 RadioButton1.Checked = True 

simply sets the first radio button in the operator list (that is, the addition operator) to True .

That's all there is to it. A sample run of the program is shown in Figure 9.2.

Figure 9.2. A sample run of the arithmetic operators program, showing the result of a modulus operation.

graphics/09fig02.jpg

The sample run shows that 23 Mod 7 is 2 . Recall that the modulus operator returns the remainder of division; you can see the result is correct. You should experiment with the program, especially the modulus and integer division operators, to make sure you understand how they work.



Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net