You are on page 1of 7

Form 2 CS

Visual Basic is a programming language and development environment created by


Microsoft. ... Visual Basic provides a graphical user interface GUI that allows the
developer to drag and drop objects into the program as well as manually write program
code.
Your first program - Write a visual basic program to print a string “Hello World”

Start a new Project and select Console Application. Type the following code:
module module1
sub main()
console.writeline("Hello World!")
console.readKey()
end sub
end module

(Press F5 to run the code)

Tasks
0.1 – Write a program that displays the message “Hello Dave”
0.2 – Write a program that displays the message “My name is Dave and I live in Brussels” (replace Dave
and Brussels with your own information)
0.3 – Write a program that displays the lyrics to your national anthem. Each line of the song should be
on a fresh line.

Variables
A variable is used to temporarily store a piece of data.
For example:

Dim number1 As Integer = 10

In the code above the variable is called number1 and the value it is storing is 10. Variables can hold
any type of data. Using variables makes it easier for people to understand what is going on. In Visual
Basic you need to define a variable before you can use it. To do this you type Dim before the variable
name the first time you use it. You should then say what type of data you think it is. In this
example number1 is an integer therefore the code to define a variable called number1 as an integer
is Dim number1 as Integer
For example:

Dim cost As Decimal = 15.5


Dim VAT As Decimal = 3.1

Variables
Often, in VBA, we’d like to dedicate a place in memory to a variable – a dedicated value your code can
modify. These should always be declared at the beginning of a program. For example if we want to
declare the variable “x” as an integer, we type in the following line:
Dim x As Integer
3.1 Types of Variables
The following table summarizes the most common types of variables you’ll need to declare:

Visual Basic naming rules


Use the following rules when you name procedures, constants, variables,
and arguments in a Visual Basic module:

 You must use a letter as the first character.


 You can't use a space, period (.), exclamation mark (!), or the characters @, &, $, # in the name.
 Name can't exceed 255 characters in length.
 Generally, you shouldn't use any names that are the same as the function, statement, method,
and intrinsic constant names used in Visual Basic or by the host application. Otherwise you end
up shadowing the same keywords in the language. To use an intrinsic language function,
statement, or method that conflicts with an assigned name, you must explicitly identify it.
Precede the intrinsic function, statement, or method name with the name of the
associated type library. For example, if you have a variable called Left, you can only invoke
the Left function by using VBA.Left.
 You can't repeat names within the same level of scope. For example, you can't declare two
variables named age within the same procedure. However, you can declare a private variable
named age and a procedure-level variable named age within the same module.

Constant variable
Constant is a value that cannot be reassigned. A constant is immutable and cannot be
changed. There are some methods in different programming languages to define a
constant variable. Most uses the const keyword. Using a const keyword indicates that
the data is read-only. We can use the const keyword in programming languages such
as C++, java script.

Example Program - Constants


'a constant is a value that doesn't change
'using them greatly improves the readability of your code
'number constants
Const conPi = 3.14159265358979
Const conMaxPlanets As Integer = 9
'string constants
Const conVersion = "07.10.A"
Const conCodeName = "Enigma"
'try assigning a new value to one of your constants
'to make a constant available to the whole program "Public" should precede it
'and it should be placed inside the module but outside the procedure
'try it
'there are string constants created by VB
Console.WriteLine("Pi is" & conPi)
Console.WriteLine("Pi is" & vbCr & conPi)
Console.WriteLine("Pi is" & vbTab & conPi)

Example Program - Arithmetic - +, –, /, x, DIV, MOD


Module Module1
Sub Main()
Dim number1 As Integer
Dim number1 As Integer
Dim total As Integer
Console.WriteLine("Enter first number")
number1 = Console.ReadLine()
Console.WriteLine("Enter second number")
number2 = Console.ReadLine()
total = number1 + number2
Console.WriteLine("The total is " & total)
Console.ReadKey()
End Sub
End Module
Write a VB program to convert Celsius to Fahrenhei

Private Sub Command1_Click()


Dim c, f As Integer
c = Text1.Text
f = 9 * c / 5 + 32
Text2.Text = f
End Sub

VB.Net program to check the given number is


even or odd
Here, we will read a number from the user and then find the given number is EVEN or
ODD and then print an appropriate message on the console screen.

Program/Source Code:

The source code to check the given number is even or odd is given below. The given
program is compiled and executed successfully.

'VB.Net program to check the given number is even or odd.

Module Module1

Sub Main()
Dim num As Integer = 0

Console.Write("Enter number: ")


num = Integer.Parse(Console.ReadLine())

If num Mod 2 = 0 Then


Console.WriteLine("Given number is EVEN")
Else
Console.WriteLine("Given number is ODD")
End If
End Sub

End Module

Output:
Enter number: 122
Given number is EVEN
Press any key to continue . . .

VB.Net program to find the largest number


among three numbers using conditional
operator
Here, we will read three integers and check the largest number and print that on the
console screen.

Program/Source Code:

The source code to find the largest number between the two numbers is given
below. The given program is compiled and executed successfully.

'VB.Net program to find the largest number


'among three numbers.

Module Module1

Sub Main()

Dim result As Integer = 0


Dim num1 As Integer = 0
Dim num2 As Integer = 0
Dim num3 As Integer = 0

Console.Write("Enter number1: ")


num1 = Integer.Parse(Console.ReadLine())

Console.Write("Enter number2: ")


num2 = Integer.Parse(Console.ReadLine())

Console.Write("Enter number3: ")


num3 = Integer.Parse(Console.ReadLine())

result = If(
(num1 > num2 AndAlso num1 > num3), num1,
If((num2 > num1 AndAlso num2 > num3), num2, num3)
)
Console.WriteLine("Largest Number is: {0}", result)
Console.ReadLine()
End Sub

End Module

Output:

Enter number1: 10
Enter number2: 30
Enter number3: 20
Largest Number is: 30

VB.Net program to calculate the perimeter of the


circle
Here we will read the radius from the user and calculate the perimeter of the circle. The
formula of the perimeter of the circle is given below:
Area = 2 * 3.14 * radius

Program/Source Code:

The source code to calculate the perimeter of the circle is given below. The given
program is compiled and executed successfully.

'VB.Net program to calculate the perimeter


'of the circle.

Module Module1

Sub Main()

Dim radius As Single = 0.0F


Dim perimeter As Single = 0.0F

Console.Write("Enter the radius:")


radius = Single.Parse(Console.ReadLine())

perimeter = 2 * 3.14F * radius

Console.WriteLine("Perimeter of Circle: {0}", perimeter)


End Sub

End Module

Output:

Enter the radius:3.5


Perimeter of Circle: 21.98
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains the Main() method.


In the Main() method we created two local variables radius and perimeter that are
initialized with 0.0F. After that, we read the value of radius from the user.

area = 2 * 3.14F * radius

In the above code, we calculated the perimeter of the circle, here we used the value of PI
that is "3.14". After that, we printed the value of perimeter on the console screen.

You might also like