0% found this document useful (0 votes)
24 views7 pages

First VB.Net Program Guide

This document provides an introduction to Visual Basic programming and includes examples of writing simple Visual Basic programs to print messages, perform arithmetic operations, and calculate values like temperature conversion, even/odd determination, largest of three numbers, and circle perimeter. It also covers Visual Basic variable declaration and naming conventions. Tasks are provided at the end for the reader to write additional simple programs.

Uploaded by

Josephat Mugumba
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

First VB.Net Program Guide

This document provides an introduction to Visual Basic programming and includes examples of writing simple Visual Basic programs to print messages, perform arithmetic operations, and calculate values like temperature conversion, even/odd determination, largest of three numbers, and circle perimeter. It also covers Visual Basic variable declaration and naming conventions. Tasks are provided at the end for the reader to write additional simple programs.

Uploaded by

Josephat Mugumba
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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()
[Link]("Hello World!")
[Link]()
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 [Link].
 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
[Link]("Pi is" & conPi)
[Link]("Pi is" & vbCr & conPi)
[Link]("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
[Link]("Enter first number")
number1 = [Link]()
[Link]("Enter second number")
number2 = [Link]()
total = number1 + number2
[Link]("The total is " & total)
[Link]()
End Sub
End Module
Write a VB program to convert Celsius to Fahrenhei

Private Sub Command1_Click()


Dim c, f As Integer
c = [Link]
f = 9 * c / 5 + 32
[Link] = f
End Sub

[Link] 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.

'[Link] program to check the given number is even or odd.

Module Module1

Sub Main()
Dim num As Integer = 0

[Link]("Enter number: ")


num = [Link]([Link]())

If num Mod 2 = 0 Then


[Link]("Given number is EVEN")
Else
[Link]("Given number is ODD")
End If
End Sub

End Module

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

[Link] 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.

'[Link] 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

[Link]("Enter number1: ")


num1 = [Link]([Link]())

[Link]("Enter number2: ")


num2 = [Link]([Link]())

[Link]("Enter number3: ")


num3 = [Link]([Link]())

result = If(
(num1 > num2 AndAlso num1 > num3), num1,
If((num2 > num1 AndAlso num2 > num3), num2, num3)
)
[Link]("Largest Number is: {0}", result)
[Link]()
End Sub

End Module

Output:

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

[Link] 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.

'[Link] program to calculate the perimeter


'of the circle.

Module Module1

Sub Main()

Dim radius As Single = 0.0F


Dim perimeter As Single = 0.0F

[Link]("Enter the radius:")


radius = [Link]([Link]())

perimeter = 2 * 3.14F * radius

[Link]("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.

Common questions

Powered by AI

To check if a number is even or odd in Visual Basic, you can use the modulus operator ('Mod'). If a number divided by 2 leaves a remainder of 0, it is even; otherwise, it is odd. Here's an example: module Module1 sub Main() Dim num As Integer 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 .

Visual Basic can handle arithmetic operations using operators like +, -, *, /, DIV, and MOD. A common pitfall is not accounting for operator precedence, which can lead to unexpected results unless parentheses are used. Additionally, dividing by zero can cause runtime errors, and incorrect data type usage may result in precision loss in division operations, especially when using integers instead of floating-point types. Careful handling of data types and mathematical expressions is crucial to avoid these pitfalls .

Constants in programming are values that cannot be reassigned after their declaration, making them immutable. In Visual Basic, constants enhance code readability and prevent accidental changes to values that are intended to remain static. They are typically used for fixed values that are reused throughout an application, such as mathematical constants or version control strings. Constants are defined using the 'Const' keyword, which indicates that the associated data is read-only .

In Visual Basic, variables must be declared with a specific type using the 'Dim' statement before use. This differs from some programming languages where variables can be used without prior declaration or where typing is inferred. Proper variable declaration is crucial in Visual Basic to ensure memory is allocated correctly and to avoid runtime errors. It clarifies the programmer's intentions, thereby improving code readability and maintainability .

To calculate the perimeter of a circle in Visual Basic, use the formula 2 * PI * radius. The program should read the radius from the user. It's important to consider data types, as radius might require a type capable of storing decimals like 'Single'. An example program is: module Module1 sub Main() Dim radius As Single Console.Write("Enter the radius:") radius = Single.Parse(Console.ReadLine()) Dim perimeter As Single = 2 * 3.14 * radius Console.WriteLine("Perimeter of Circle: {0}", perimeter) End Sub End Module .

In Visual Basic, the 'If' conditional operator can be leveraged to determine the largest number among three inputs through nested conditions. Here's an illustrative example: module Module1 sub Main() Dim result, num1, num2, num3 As Integer 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) End Sub End Module. This method efficiently evaluates the largest number using logical comparisons .

The graphical user interface (GUI) in Visual Basic allows developers to visually design applications by dragging and dropping objects into the program. This enhances the development experience by simplifying the process of building the user interface and reducing the amount of manual code that must be written. The GUI approach makes the development process quicker and more intuitive, particularly for developers who may not be as comfortable with extensive coding from scratch .

Ignoring Visual Basic's variable naming rules can lead to significant issues in program functionality. Names that clash with intrinsic language functions, methods, or keywords may cause unexpected behavior or errors if they are not explicitly specified using their type library. Overly lengthy names can impact readability and maintainability. Using prohibited characters can lead to syntax errors during compilation, and failing to adhere to unique scope-level naming can cause conflicts resulting in incorrect logic or runtime errors .

To create a simple Visual Basic program that prints 'Hello World' to the console, start a new project and select 'Console Application'. Within the module, write the following code: module module1 sub main() console.writeline("Hello World!") console.readKey() end sub end module Press F5 to run the code and see 'Hello World' printed on the console .

When naming variables in Visual Basic, you must start with a letter, and you cannot use spaces, periods, exclamation marks, or characters like @, &, $, and #. Names must not exceed 255 characters and should not conflict with any Visual Basic function, statement, or method names. If a conflict is unavoidable, explicitly specify the function with the associated type library, for example, using VBA.Left for a function named 'Left' .

You might also like