Write a program in Visual Basic
Design a calculator. Take input as (1/2 / 3/4 /5)
1 - Add
2 - Subtract
3 -Multiply
4 - Divide
5 - Mod
Answers
Simple Calculations
Just having some values lying around does not make a program. You need to do something with the values. You can perform math on the values, or you can do something more substantial. Similarly, to make your programs more understandable, you will often need to use or create procedures. Visual Basic .NET will provide some of these procedures; you will write others. These procedures range from the operators, which provide many of the common mathematical calculations, to more complex functions that could affect strings or numbers.
Using Operators
In Visual Basic .NET, operators perform simple calculations and similar "functions." Most of the operators in Visual Basic .NET should be familiar to you as common algebraic symbols. However, some of them are unique to programming. Table 3.6 lists the most commonly used operators.
Table 3.6 Common Operators in Visual Basic .NET
Operator
Use
Example
=
Assigns one value to another
X = 6
+
Adds two values
Y = X + 7 (Y holds 13)
-
Subtracts one value from another
Y = X – 4 (Y holds 2)
*
Multiplies two values
Y = X * 2 (Y holds 12)
/
Divides one value by another
Y = X / 2 (Y holds 3)
\
Divides one value by another, but only returns a whole number
Y = X \ 3 (Y holds 1)
Mod
Short for modulus; returns the remainder for a division
Y = X Mod 3 (Y holds 2)
&
Combines two strings
S = "Hello " & "World" (S holds "Hello World")
+=
Shorthand for adds a value and assigns the result
X += 2 (X holds 8)
-=
Shorthand for subtracts a value and assigns the result
X –= 3 (X holds 5)
*=
Shorthand for multiplies a value and assigns the result
X *= 6 (X holds 30)
/=
Shorthand for divides by a value and assigns the result
X /= 5 (X holds 6)
&=
Shorthand for combines with a string and assigns the result
S &= ", John" (S holds "Hello World, John")
^
Raises one value to the power of an exponent
3^4 (3 to the power of 4, returns 81)
Built-In Functions
In addition to the functions provided by the .NET Framework, Visual Basic .NET has many built-in functions. These functions provide many useful capabilities, including conversion from one data type to another, mathematical calculations, string manipulation, and so on. You should know about some of these functions to be able to get around in Visual Basic .NET.
Conversion Functions
Some of the most important functions available to you in Visual Basic are the conversion functions. They enable you to take one type of data and convert it to another. With Visual Basic .NET, conversion functions have become even more important because this version of Visual Basic is much stricter about data types, and it does not automatically convert one type into another as previous versions did.
CAUTION
If you want Visual Basic .NET to automatically convert data types for you, you can turn off the strict type checking by adding Option Strict Off to the top of your files. You should know, however, that this could lead to unexpected results in your code (that is, bugs) if Visual Basic .NET converts a variable when you don't expect it to.
The conversion functions in Visual Basic .NET all begin with the letter "C" (as in conversion), and end with an abbreviated form of the new type. In addition, there is a generic conversion function, CType, which can convert to any type. Table 3.7 describes the main conversion functions.
Table 3.7 Conversion Functions
Function
Description
CBool
Converts to a Boolean. Anything that evaluates to False or 0 will be set to False; otherwise, it will be True.
CByte
Converts to a Byte. Any value greater than 255, or any fractional information, will be lost.
CChar
Converts to a single character. If the value is greater than 65,535, it will be lost. If you convert a String, only the first character is converted.
CDate
Converts to a Date. One of the more powerful conversion functions, CDate can recognize some of the more common formats for entering a date.
CDbl
Converts to a Double.
CInt
Converts to an Integer. Fractions are rounded to the nearest value.
CLng
Converts to a Long. Fractions are rounded to the nearest value.
CSht
Converts to a Short. Fractions are rounded to the nearest value.
CStr
Converts to a String. If the value is a Date, this will contain the Short Date format.
CType
Converts to any type. This is a powerful function that enables you to convert any data type into any other type. Therefore, the syntax for this function is slightly different than the others.
The syntax for CType is
oNewVariable = CType(oOldVariable, NewType)