Tech Blog And Frequent Rants About Nothing

# Creating a Calculator | VB.Net Part3 (Wrapping Up)

Now that you’ve finally made it here, you’ve created a calculator but there’s still some things I wish to help you fix. Quite frankly, we have to fix a majorÂ redundancyÂ in our code. numFirst = numFirst operator numSecond is the basis of our whole calculator when we wish to do this continuious calculation. We’ve used it 5 times which means we spent too much time re-writing the same code over and over. With re-writing code, you’re prone to creating a flaw somewhere, either a mistype or a logical error, something. So instead, lets create a function that does it for us and we can just call when we need to use it.

I call my function CalculateIt()

Private Function CalculateIt(ByVal n1 As Double, n2 As Double, ByVal op As String) As Double
Select Case op
Case “+”
Return n1 + n2
Case “-”
Return n1 – n2
Case “*”
Return n1 * n2
Case “/”
Return n1 / n2
End Select
End Function

It should seem pretty self-explanatory by now. We’re taking the value of n1 (this will be numFirst/num1) calculating it with n2 (numSec/num2) based upon op (operator/oper) and then return the result. The result will be a Double.

Now we can change our btnAdd/Minus/etc to say
Else
num1 = CalculateIt(CDbl(num1), CDbl(TextBox1.Text), oper)
oper = “+”
TextBox1.Clear()
End If

and our btnEquals will have
TextBox1.Text = CStr(CalculateIt(CDbl(num1), CDbl(num2), oper))

And finally, you are now done. You could start putting in Error Handlers, Decimal points, or even be fancy with the Memory functions. But that’s outside the scope of this article for right now. If you’d like me to continue on later with a Scientific calculator, make sure that you leave me a message and +1 this article on Google+ or Share on Facebook.

Public Class Form1
‘////////////Our Declares////////////
Dim num1, num2 As String
Dim oper As String
‘////////////End of Decalres/////////
‘////////////The Number Buttons//////
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.AppendText(“1”)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.AppendText(“2”)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.AppendText(“3”)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox1.AppendText(“4”)
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
TextBox1.AppendText(“5”)
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
TextBox1.AppendText(“6”)
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
TextBox1.AppendText(“7”)
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
TextBox1.AppendText(“8”)
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
TextBox1.AppendText(“9”)
End Sub
Private Sub btnZero_Click(sender As Object, e As EventArgs) Handles btnZero.Click
TextBox1.AppendText(“0”)
End Sub
‘///////////End of Number Buttons//////
‘///////////Control Buttons +-/*=//////
If num1 = “” Then
num1 = TextBox1.Text
oper = “+”
TextBox1.Clear()
Else
num1 = CalculateIt(CDbl(num1), CDbl(TextBox1.Text), oper)
oper = “+”
TextBox1.Clear()
End If
End Sub
Private Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click
If oper = “” Then
MsgBox(“You must enter a second number”)
Exit Sub
End If
num2 = TextBox1.Text
TextBox1.Text = CStr(CalculateIt(CDbl(num1), CDbl(num2), oper))
End Sub
Private Sub btnMinus_Click(sender As Object, e As EventArgs) Handles btnMinus.Click
If num1 = “” Then
num1 = TextBox1.Text
oper = “-”
TextBox1.Clear()
Else
num1 = CalculateIt(CDbl(num1), CDbl(TextBox1.Text), oper)
oper = “-”
TextBox1.Clear()
End If
End Sub
Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.Click
If num1 = “” Then
num1 = TextBox1.Text
oper = “*”
TextBox1.Clear()
Else
num1 = CalculateIt(CDbl(num1), CDbl(TextBox1.Text), oper)
oper = “*”
TextBox1.Clear()
End If
End Sub
Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click
If num1 = “” Then
num1 = TextBox1.Text
oper = “/”
TextBox1.Clear()
Else
num1 = CalculateIt(CDbl(num1), CDbl(TextBox1.Text), oper)
oper = “/”
TextBox1.Clear()
End If
End Sub
Private Sub btnclear_Click(sender As Object, e As EventArgs) Handles btnclear.Click
TextBox1.Clear()
num1 = “”
num2 = “”
oper = “”
End Sub
‘///////////End Of Control Buttons/////
‘///////////Our Calculator Function////
Private Function CalculateIt(ByVal n1 As Double, n2 As Double, ByVal op As String) As Double
Select Case op
Case “+”
Return n1 + n2
Case “-”
Return n1 – n2
Case “*”
Return n1 * n2
Case “/”
Return n1 / n2
End Select
End Function
‘///////////End of Calculator Funct////
End Class

This site uses Akismet to reduce spam. Learn how your comment data is processed.