Coding a calculator in Visual Basic is one of the easiest ways for a beginning programmer to become familiar with the Visual Basic programming language. A basic calculator program will have a series of button controls and a text box control on the form. The programmer has the option of adding additional buttons to preform scientific calculations in addition to the standard addition, subtraction, multiplication and division.
Other People Are Reading
Basic or Scientific
Decide whether you are going to develop a basic or scientific calculator. The basic calculator is significantly easier to code than the scientific calculator. A scientific calculator will also be able to compute pi, sine, cosine, square root and tangent. Both types of calculators will have the same layout that includes several button controls and a text box control. However, developing a scientific calculator is significantly easier to do in VB.NET than VB 6.0 because VB.NET has the Math methods built in and available in the Math class.
It is more efficient to create one button control, name it and then copy and paste it to the form. The VB programming software will ask if you'd like to create a control array. Answering "yes" will create copies of the button control with an indexed array. The buttons will be named similar to "cmdBtn(0)." This will allow you to write the code for each button in one subroutine. If you'd prefer to name each button individually and not use an array, you can still put the code in one subroutine in VB.NET by adding the "Handles" word to the subroutine. The other button names are added after "Handles."
The basic calculator is as simple as performing one of four optional calculations on the first and second numbers entered by the user. The scientific calculator is also quite simple when using the Math class available in VB.NET. The Math methods are accessed by including the line "Imports System" at the start of your VB.NET application. This makes the Math class accessible to the program. The desired calculation is then accessed with "Math." For example, getting the cosine of a number is as simple as "Math.Sin(anglevalue)" where "anglevalue" is a variable defined with the datatype "Double".
When dealing with numbers, there are a few errors that could occur and thus need to be handled in your application. The advanced mathematical functions available in VB.NET could return an invalid number. The types of invalid numbers are "Nan" (not a number), "NegativeInfinity" (results from dividing a negative number by zero) or "PositiveInfinity" (results dividing a positive number by zero). VB 6.0 will return a "run-time error" with a description of "Division by zero" for both positive and negative division by zero. If a calculation is attempted in VB 6.0 with a non-numeric value, a "run-time error" with a description of "Type mismatch" occurs.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for