# Visual Basic Arithmetic Operators

“Arithmetic Operators” is a rather obtuse name for very simple maths such as addition, subtraction, multiplication and division.   Have I mentioned that computer programmers love to give a simple concept as intimidating a name as possible?  Well they do, time and time again.  Perhaps it’s a means to blind you with science, I’m not sure.  I’m sure they’d blame the mathematicians who invented the term but come on guys!  You had the chance to make coding far more accessible to the layman.  I ought to be thankful of course, I wouldn’t be here if they’d made programming simple to pick up, but still…

As you’d expect, it is extremely easy to implement these Arithmetic Operators in Visual Basic.  The syntax for the operators is:

• – for subtraction
• / for division
• * for multiplication

To see these in action we’re going to create a simple calculator.  Create a new Project called, imaginatively enough, Calculator

• Extend Form1 so it’s slightly wider than by default
• Add 4 textboxes called TextBoxNumberOne, TextBoxOperator, TextBoxNumberTwo and TextBoxResult.
• Add a button called ButtonCalculate with the Text “Calculate”
• Style the Form so  it so it looks as follows:

In the ButtonCalculate Click Event add the following code

```Dim numberOne As Integer = CInt(TextBoxNumberOne.Text)
Dim numberTwo As Integer = CInt(TextBoxNumberTwo.Text)

If TextBoxOperator.Text = "+" Then
TextBoxResult.Text = CStr(numberOne + numberTwo)
ElseIf TextBoxOperator.Text = "-" Then
TextBoxResult.Text = CStr(numberOne - numberTwo)
ElseIf TextBoxOperator.Text = "*" Then
TextBoxResult.Text = CStr(numberOne * numberTwo)
Else
TextBoxResult.Text = CStr(numberOne / numberTwo)
End If
```

The code essentially reads the two numbers in TextBoxNumber One and Two and assigns them to two variables numberOne and numberTwo.  It looks at TextBoxOperator to see what to do to these numbers (add, subtract, multiply or divide).  It performs this operation and shows the result in TextBoxResult.

For example, if there is a + in the textbox, the code adds the two numbers and show the result.   If TextBoxOperator is – then subtract.  And so on.  Even in the written word I am using the IF Statement!

In the code you’ll see a reference to CInt and CStr.  These functions convert text to integer (CInt) and number to text (CStr).  Str is shorthand for String; another obtuse programmer’s word for text, used in virtually all languages.  Without these conversions the code would attempt to perform maths on the contents of a textbox.  The contents of a textbox are always text, and you can’t e.g. add two words.  After the maths has completed, the CStr makes sure that we’re putting text rather than numbers into the textbox.  If we didn’t use these functions, Visual Basic would kick up a fuss.

Run the program and play with it.  You’ve created a simple calculator in 12 lines of code!

See if you can break it.  We’ve made some pretty fundamental assumptions in this routine.  See if you can spot them and break them.  Some people are naturally excellent at doing this and often I recommend they look at becoming testers, after they’ve learnt to code of course.

Take a look at the video to see how to break this routine!