#Region " Methods "
''' <summary>
''' Here i've established 4 very simple functions for preforming our
''' 4 basic math calculations. A function is good for this, as we can
''' pass in 2 variables and recieve an answer in return!
''' </summary>
Private Function mAdd(ByVal frstValu As Object, ByVal scndValue As Object)
' These first 2 variables ensre that our values passed in are in correct format for calculating
Dim tmpFrstValue As Double = CDbl(frstValu)
Dim tmpScndValue As Double = CDbl(scndValue)
' This 3rd variable is our math!
' Something to note about vb, when placed between 2 numeric values,
' a math symbol (such as the 4 in this program) is translated
' literally.
Dim tmpResult As Double = tmpFrstValue + tmpScndValue
' This is a real difference between a Function and a Sub Routine.
' Functions should always 'return' a value to the place where it's called.
' However, a sub will only return ZERO values, as it simply 'does work'
Return tmpResult
End Function
Private Function mSubtract(ByVal frstValu As Object, ByVal scndValue As Object)
Dim tmpFrstValue As Double = CDbl(frstValu)
Dim tmpScndValue As Double = CDbl(scndValue)
Dim tmpResult As Double = tmpFrstValue - tmpScndValue
Return tmpResult
End Function
Private Function mMultiply(ByVal frstValu As Object, ByVal scndValue As Object)
Dim tmpFrstValue As Double = CDbl(frstValu)
Dim tmpScndValue As Double = CDbl(scndValue)
Dim tmpResult As Double = tmpFrstValue * tmpScndValue
Return tmpResult
End Function
Private Function mDivide(ByVal frstValu As Object, ByVal scndValue As Object)
Dim tmpFrstValue As Double = CDbl(frstValu)
Dim tmpScndValue As Double = CDbl(scndValue)
Dim tmpResult As Double = tmpFrstValue / tmpScndValue
Return tmpResult
End Function
''' <summary>
''' This is our main Sub Routine. Here is where most of the 'work' happens
''' after a calculation button is pressed. This is established to keep from
''' having 'repetative' throughout each math button click.
''' </summary>
''' <param name="value">This is a prime example of a coder defined parameter.
''' Keep in mind, when writing your own methods, you can establish as many
''' parameters as you feel you will need.
''' This parameter provides a simple way to help deteremine
''' what math to perform, as that is the key difference between the
''' objects using this method.</param>
Sub doWork(ByVal value As calc)
' First, we set our universal 'what math' parameter,
' so that is any other method should need to know
' its value, we don't have to pass it as another parameter
calcValue = value
' Next, before calculating anything, we need to make sure the
' user ment to calculate somthing. This is accounting for user
' error. This simple practice helps in every prgrom to prevent
' such errors as trying to add 2 objects that are empty.
If Not txtInput.Text = "" Then
' With the opening work out of the way, we start on the meat
' 1st, for our case, we'll display the math about to take place
' in our RichTextBox
' The text being shown is as follows
' result = the current value of our result property, as it is the first number in the math equation
' " " = simply puts a 'space' between first value and the symbol
' selSymbol = a simple function for getting the right symbol to display
' This relates back to our 'universal' variable established at the
' start of this Sub. Here is one reason for the esablishment of that
' as a universal value. If not universal, then we'd be passing our
' parameter all over again.
rtxShowWork.AppendText(result & " " & selSymbol() & " " & txtInput.Text.ToString)
' Now, we begin to do the actual math (for more information, see selMath sub below)
result = selMath()
' Now, we display our new results to the user
lblResult.Text = result.ToString
' Also show this result in a manner to complete the last line of math in our RichTextBox
rtxShowWork.AppendText(" = " & result.ToString)
' Here we add a 'line break' to our RichTextBox for the purpose of preparing it for the next line of math
rtxShowWork.AppendText(Environment.NewLine)
End If
' Finally, clear our input TextBox and reset its focus
txtInput.Text = ""
txtInput.Focus()
End Sub
''' <summary>
''' This is the first simple function referred to in the last Sub Routine.
''' It makes use of the previously established universal value based off of
''' our math enum.
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Function selSymbol() As String
' A SelectCase Statement is a good way to clean up long, nasty IfElse Statements
' This statement returns a string value representing the symbol of the calculation
' established by our universal 'calcValue'
Select Case calcValue
Case calc.Add
Return "+"
Case calc.Subtract
Return "-"
Case calc.Multiply
Return "*"
Case calc.Divide
Return "/"
End Select
' It's always good practice to end your function with a final return, so that if no
' other values are returned, it has a final result of some establishment
' Altho, not having a final return is not bad, as the final return will generally
' be the same thing as Return Nothing
Return Nothing
End Function
''' <summary>
''' Here, we make further use of our universal calcValue to determine which
''' math function to use on our 2 universal number values. Perhaps it should be
''' noted that, while we did not explicitly pass our new Text to a universal value,
''' reading the textbox before its been cleared can still be considered a universal
''' value for the second number in our math equation.
''' </summary>
Function selMath() As Double
' A Simple SelectCase Statement like in the previous Function,
' however, this time we will return the result of one of
' the previously established math functions.
' Of course, what result we return is based on the current case
' of our universal calcValue property.
Select Case calcValue
Case calc.Add
Return mAdd(result, txtInput.Text.ToString)
Case calc.Subtract
Return mSubtract(result, txtInput.Text.ToString)
Case calc.Multiply
Return mMultiply(result, txtInput.Text.ToString)
Case calc.Divide
Return mDivide(result, txtInput.Text.ToString)
End Select
End Function
#End Region
End Class