MDAS Calculation Using a Class

In this tutorial I will teach you how to create the MDAS calculation within the class using Visual Basic 2008. This will help you organize your code and minimize bunch of codes in your Windows Form. MDAS stands for Multiplication, Division, Addition and Subtraction. Let’s begin: Open Visual Basic 2008, create a Project named “MDAS” and set up your Form just like this. MDAS FORM Add a new file which is a class and name it “calculate”. After that, do this code inside the class.
  1. Public Class calculate
  2. 'declare a private variable as a double
  3. Private _num1 As Double
  4. Private _num2 As Double
  5. Private _total As Double
  6.  
  7. 'set a readonly property for the total of the MDAS
  8. ReadOnly Property total() As Double
  9. Get
  10. Return _total 'return the total
  11. End Get
  12. End Property
  13. 'create a property for a private variable so that you can access it
  14. Property num1() As Double
  15. Get
  16. Return _num1 'return the first value
  17. End Get
  18. Set(ByVal value As Double)
  19. _num1 = value 'set the first value
  20. End Set
  21. End Property
  22. Property num2() As Double
  23. Get
  24. Return _num2
  25. End Get
  26. Set(ByVal value As Double)
  27. _num2 = value
  28. End Set
  29. End Property
  30.  
  31. 'create the sub procedures of the MDAS
  32. Public Sub multiply()
  33. 'formula of multiplication
  34. _total = num1 * num2
  35. End Sub
  36. Public Sub divide()
  37. 'formula of division
  38. _total = num1 / num2
  39. End Sub
  40. Public Sub add()
  41. 'formula of addition
  42. _total = num1 + num2
  43. End Sub
  44. Public Sub subtract()
  45. 'formula of subtraction
  46. _total = num1 - num2
  47. End Sub
  48. End Class
Go back to the Design Views, double click the Form and create a sub procedure for the clicking event of the radio button.
  1. Public Class Form1
  2.  
  3. 'create a sub procedure for the events of clicking the radio button that handles all of it.
  4. Private Sub RadioButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
  5. Handles Radio_Subtaction.Click, Radio_Multiplication.Click, Radio_Division.Click, Radio_Addition.Click
  6.  
  7. 'call a constructor method and return to cal as an instance of a class
  8. Dim cal As calculate = New calculate()
  9.  
  10. 'declaring the string variable represent as a textbox
  11. Dim txtnum1 As String = TextBox1.Text
  12. Dim txtnum2 As String = TextBox2.Text
  13. 'declaring the double variable
  14. Dim dbl_val1 As Double
  15. Dim dbl_val2 As Double
  16.  
  17. If IsNumeric(txtnum1) And IsNumeric(txtnum2) Then 'check if the textbox has a numeric value
  18. 'convert the string to double
  19. dbl_val1 = Double.Parse(txtnum1)
  20. dbl_val2 = Double.Parse(txtnum2)
  21.  
  22. 'get the value of the converted variable
  23. 'to pass it into the variable in the class
  24. cal.num1 = dbl_val1
  25. cal.num2 = dbl_val2
  26.  
  27. 'the condition is, if the radiobutton is clicked,
  28. 'the operation of MDAS executes.
  29. If Radio_Multiplication.Checked Then
  30. 'result:
  31. cal.multiply() 'call a subname in a class for multiplying
  32. ElseIf Radio_Division.Checked Then
  33. 'result:
  34. cal.divide() 'call a subname in a class for dividing
  35. ElseIf Radio_Addition.Checked Then
  36. 'result:
  37. cal.add() 'call a subname in a class for adding
  38. ElseIf Radio_Subtaction.Checked Then
  39. 'result:
  40. cal.subtract() 'call a subname in a class for subtracting
  41. End If
  42. Else
  43. 'the result is:
  44. 'if the textbox is empty or has a string value
  45. TextBox3.Text = "Enter a number"
  46. Return
  47. End If
  48. 'put the result of the MDAS to a textbox.
  49. TextBox3.Text = cal.total()
  50. End Sub
  51. End Class

Add new comment