Modify a text file: $/0924-tip.vb
$/0924-tip.vb
Public Class Form1 '******** I/O METHODS ******* Sub say(ByVal s As String) '**** Output a string to the listbox. Me.output.Items.Add(s) 'Display the string s in listbox. End Sub Sub saywhat(ByVal s As String, ByVal r As Double) '**** Output a string and a value to the listbox. Me.output.Items.Add(s & " " & Format(r)) 'Display string s & r in listbox. End Sub Sub saymsg(ByVal what As String, ByVal r As Double) '**** Output a string and a value to the listbox. Dim s As String s = "The " & what & " is " Me.output.Items.Add(s & " " & Format(r)) 'Display string s & r in listbox. End Sub Function getFirst() As Double 'Return the first value from form (textbox) Dim result As Double result = CDbl(Me.txtFirst.Text) Return result End Function Function getSecond() As Double 'Return the second value from form (textbox) Return CDbl(Me.txtSecond.Text) End Function '****** Math methods ***** Function diff(ByVal a As Double, ByVal b As Double) As Double 'Return the difference between a and b Dim result As Double result = a - b Return result End Function Function sum(ByVal a As Double, ByVal b As Double) As Double 'Return the sum of a and b Dim result As Double result = a + b Return result End Function Private Sub btnDiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiv.Click '****** Divide, but check for div by zero. Output result (or err msg). Dim a, b As Double ' First and second inputs (from txtFirst, txtSecond) Dim result As Double ' Result of operation. Dim msg As String 'The output string will be composed here. Dim what As String = "quotient" 'Result type. '1. INPUT: Get a, b a = CDbl(Me.txtFirst.Text) b = CDbl(Me.txtSecond.Text) '2. PROCESS: Compute the quotient, but first check for /0 If (b <> 0) Then result = a / b '-- msg = "The quotient is " & Format(result) msg = "The " & what & " is " & Format(result) Else msg = "!!!! Denominator cannot be zero!" End If '3. OUTPUT: Show the answer. say(msg) End Sub Private Sub btnMul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMul.Click '****** Multiply 2 #s. Output result (or err msg). Dim a, b As Double ' First and second inputs (from txtFirst, txtSecond) Dim result As Double ' Result of operation. Dim msg As String Dim what As String = "product" '0. INIT: msg = "The " & what & " is " '1. INPUT: Get a, b a = CDbl(Me.txtFirst.Text) b = CDbl(Me.txtSecond.Text) '2. PROCESS: Compute the quotient, but first check for /0 result = a * b '3. OUTPUT: Show the answer. saywhat(msg, result) End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click '****** Add 2 #s. Output result. Dim a, b As Double ' First and second inputs (from txtFirst, txtSecond) Dim x As Double ' Result of operation. Dim what As String = "sum" '1. INPUT: a, b a = getFirst() b = getSecond() '2. PROCESS: Add a, b x = sum(a, b) '3. OUTPUT: Show the answer. saymsg(what, x) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '****** Subtract 2 #s. Output result. Dim a, b As Double ' First and second inputs (from txtFirst, txtSecond) Dim x As Double ' Result of operation. Dim what As String = "difference" '1. INPUT: a, b a = getFirst() b = getSecond() '2. PROCESS: Add a, b x = diff(a, b) '3. OUTPUT: Show the answer. saymsg(what, x) End Sub End Class