Introduction

This project showcases how to create a simple calculator in VB.NET, where the user inputs two numbers and selects an operation (+, -, *, /). The program then calculates and displays the result, while handling basic errors like division by zero and invalid operations.


VB.NET (Visual Basic .NET) is a versatile language widely used for creating Windows-based applications. It is known for its simplicity and ease of use, making it a great choice for beginners.

Code Breakdown

Imports and Module Setup

The program begins by importing the `System` namespace, which provides fundamental classes and base classes that define commonly-used values and reference data types. Then, we define a `Module` containing the `Main` subroutine, which serves as the entry point of the program.


Input and Operation Handling

In the `Main` method, we prompt the user to input two numbers and an operation. The program captures this input using `Console.ReadLine()` and converts the input into appropriate data types.

The `Select Case` statement is used to handle different arithmetic operations (addition, subtraction, multiplication, division), with specific checks for errors like division by zero or invalid operations.


Error Handling

For the division operation, there’s an explicit check to prevent division by zero. Additionally, an error message is displayed for invalid operations that are not
supported (e.g., entering a symbol other than +, -, *, or /).


Code Samples

VB.NET Code for the Simple Calculator

Import

This section sets up the basic structure of the program, with variable declarations for `num1`, `num2`, `result`, and `operation`. It also prints out a simple title for the calculator.

calcimport

Get Input

This block takes input from the user for the two numbers and the operation they want to perform. The numbers are converted from strings to `Double` values to handle floating-point operations.

calcgets

Calculation

The `Select Case` statement executes the operation chosen by the user, such as addition or multiplication. It also includes a check for division by zero, which outputs an error message if the second number is zero.

calcfunction

Display

After performing the calculation, the result is displayed, and the program waits for the user to press a key before closing. This ensures the result is visible before the program exits.

calcdisplay

Solution Visualization

Below is an example of the printed output illustrating how the solution is presented:

calcsolution

Conclusion

This simple calculator program demonstrates the basic functionality of VB.NET by handling user input, performing operations, and managing errors. With the help of a `Select Case` statement, the program efficiently performs addition, subtraction, multiplication, and division, providing feedback for invalid inputs.


VB.NET's ease of use and powerful features make it a great choice for creating small to large-scale applications. This project can be further expanded by adding more advanced functionality, such as supporting more complex operations (e.g., square root, power) or creating a graphical interface using Windows Forms.