EZ Calc Code Code:
  1. #Region "   Methods     "
  2.  
  3.     ''' <summary>
  4.     ''' Here i've established 4 very simple functions for preforming our
  5.     ''' 4 basic math calculations. A function is good for this, as we can
  6.     ''' pass in 2 variables and recieve an answer in return!
  7.     ''' </summary>
  8.     Private Function mAdd(ByVal frstValu As Object, ByVal scndValue As Object)
  9.         '   These first 2 variables ensre that our values passed in are in correct format for calculating
  10.         Dim tmpFrstValue As Double = CDbl(frstValu)
  11.         Dim tmpScndValue As Double = CDbl(scndValue)
  12.         '   This 3rd variable is our math!
  13.         '   Something to note about vb, when placed between 2 numeric values,
  14.         '       a math symbol (such as the 4 in this program) is translated
  15.         '       literally.
  16.         Dim tmpResult As Double = tmpFrstValue + tmpScndValue
  17.         '   This is a real difference between a Function and a Sub Routine.
  18.         '   Functions should always 'return' a value to the place where it's called.
  19.         '   However, a sub will only return ZERO values, as it simply 'does work'
  20.         Return tmpResult
  21.     End Function
  22.     Private Function mSubtract(ByVal frstValu As Object, ByVal scndValue As Object)
  23.         Dim tmpFrstValue As Double = CDbl(frstValu)
  24.         Dim tmpScndValue As Double = CDbl(scndValue)
  25.         Dim tmpResult As Double = tmpFrstValue - tmpScndValue
  26.         Return tmpResult
  27.     End Function
  28.     Private Function mMultiply(ByVal frstValu As Object, ByVal scndValue As Object)
  29.         Dim tmpFrstValue As Double = CDbl(frstValu)
  30.         Dim tmpScndValue As Double = CDbl(scndValue)
  31.         Dim tmpResult As Double = tmpFrstValue * tmpScndValue
  32.         Return tmpResult
  33.     End Function
  34.     Private Function mDivide(ByVal frstValu As Object, ByVal scndValue As Object)
  35.         Dim tmpFrstValue As Double = CDbl(frstValu)
  36.         Dim tmpScndValue As Double = CDbl(scndValue)
  37.         Dim tmpResult As Double = tmpFrstValue / tmpScndValue
  38.         Return tmpResult
  39.     End Function
  40.  
  41.     ''' <summary>
  42.     ''' This is our main Sub Routine. Here is where most of the 'work' happens
  43.     ''' after a calculation button is pressed. This is established to keep from
  44.     ''' having 'repetative' throughout each math button click.
  45.     ''' </summary>
  46.     ''' <param name="value">This is a prime example of a coder defined parameter.
  47.     ''' Keep in mind, when writing your own methods, you can establish as many
  48.     ''' parameters as you feel you will need.
  49.     ''' This parameter provides a simple way to help deteremine
  50.     ''' what math to perform, as that is the key difference between the
  51.     ''' objects using this method.</param>
  52.     Sub doWork(ByVal value As calc)
  53.         '   First, we set our universal 'what math' parameter,
  54.         '       so that is any other method should need to know
  55.         '       its value, we don't have to pass it as another parameter
  56.         calcValue = value
  57.         '   Next, before calculating anything, we need to make sure the
  58.         '       user ment to calculate somthing. This is accounting for user
  59.         '       error. This simple practice helps in every prgrom to prevent
  60.         '       such errors as trying to add 2 objects that are empty.
  61.         If Not txtInput.Text = "" Then
  62.             '   With the opening work out of the way, we start on the meat
  63.             '   1st, for our case, we'll display the math about to take place
  64.             '       in our RichTextBox
  65.             '       The text being shown is as follows
  66.             '           result = the current value of our result property, as it is the first number in the math equation
  67.             '           " " = simply puts a 'space' between first value and the symbol
  68.             '           selSymbol = a simple function for getting the right symbol to display
  69.             '               This relates back to our 'universal' variable established at the
  70.             '               start of this Sub.  Here is one reason for the esablishment of that
  71.             '               as a universal value. If not universal, then we'd be passing our    
  72.             '               parameter all over again.
  73.             rtxShowWork.AppendText(result & " " & selSymbol() & " " & txtInput.Text.ToString)
  74.             '   Now, we begin to do the actual math (for more information, see selMath sub below)
  75.             result = selMath()
  76.             '   Now, we display our new results to the user
  77.             lblResult.Text = result.ToString
  78.             '   Also show this result in a manner to complete the last line of math in our RichTextBox
  79.             rtxShowWork.AppendText(" = " & result.ToString)
  80.             '   Here we add a 'line break' to our RichTextBox for the purpose of preparing it for the next line of math
  81.             rtxShowWork.AppendText(Environment.NewLine)
  82.         End If
  83.         '   Finally, clear our input TextBox and reset its focus
  84.         txtInput.Text = ""
  85.         txtInput.Focus()
  86.     End Sub
  87.  
  88.     ''' <summary>
  89.     ''' This is the first simple function referred to in the last Sub Routine.
  90.     ''' It makes use of the previously established universal value based off of
  91.     ''' our math enum.
  92.     ''' </summary>
  93.     ''' <returns></returns>
  94.     ''' <remarks></remarks>
  95.     Function selSymbol() As String
  96.         '   A SelectCase Statement is a good way to clean up long, nasty IfElse Statements
  97.         '   This statement returns a string value representing the symbol of the calculation
  98.         '       established by our universal 'calcValue'
  99.         Select Case calcValue
  100.             Case calc.Add
  101.                 Return "+"
  102.             Case calc.Subtract
  103.                 Return "-"
  104.             Case calc.Multiply
  105.                 Return "*"
  106.             Case calc.Divide
  107.                 Return "/"
  108.         End Select
  109.         '   It's always good practice to end your function with a final return, so that if no
  110.         '       other values are returned, it has a final result of some establishment
  111.         '   Altho, not having a final return is not bad, as the final return will generally
  112.         '       be the same thing as Return Nothing
  113.         Return Nothing
  114.     End Function
  115.  
  116.     ''' <summary>
  117.     ''' Here, we make further use of our universal calcValue to determine which
  118.     ''' math function to use on our 2 universal number values. Perhaps it should be
  119.     ''' noted that, while we did not explicitly pass our new Text to a universal value,
  120.     ''' reading the textbox before its been cleared can still be considered a universal
  121.     ''' value for the second number in our math equation.
  122.     ''' </summary>
  123.     Function selMath() As Double
  124.         '   A Simple SelectCase Statement like in the previous Function,
  125.         '       however, this time we will return the result of one of
  126.         '       the previously established math functions.
  127.         '   Of course, what result we return is based on the current case
  128.         '       of our universal calcValue property.
  129.         Select Case calcValue
  130.             Case calc.Add
  131.                 Return mAdd(result, txtInput.Text.ToString)
  132.             Case calc.Subtract
  133.                 Return mSubtract(result, txtInput.Text.ToString)
  134.             Case calc.Multiply
  135.                 Return mMultiply(result, txtInput.Text.ToString)
  136.             Case calc.Divide
  137.                 Return mDivide(result, txtInput.Text.ToString)
  138.         End Select
  139.     End Function
  140.  
  141. #End Region
  142.  
  143. End Class