You are on page 1of 11

VISUAL BASIC (I)

AN NAJAH NATIONAL UNIVERSITY

MANAGEMENT INFORMATION SYSTEMS


PREPARED BY :
MOHAMMED ABDEL KHALEQ DWIKAT
dwikatmo@najah.edu
dwikatmo@gmail.com
www.facebook.com/dwikatmo

25/10/2023

1. IDENTIFIERS(VARIABLES, CONSTANTS, STATIC)


2. COMMENT STATEMENTS
3. NAMING CONVENTIONS
4. DECLARING, USING & INITIALIZING VARIABLES
5. CONSTANTS IN VISUAL BASIC
6. DATA TYPES AVAILABLE IN VB.NET
7. VISUAL STUDIO 2010

AUTHOR: MOHAMMED ABDEL KHALIQ DWIKAT EMAIL:dwikatmo@gmail.com


TOPIC: VISUAL BASIC I / IDENTIFIERS DATE: 10/25/23 PAGE: 1 OF 11
IDENTIFIERS
An identifier is a name used to identify a class, variable,
function, or any other user-defined item. The basic rules for naming
classes in VB.Net are as follows:

 A name must begin with a letter that could be followed by a


sequence of letters, digits (0 - 9) or underscore (_). The first
character in an identifier cannot be a digit.
 It must not contain any embedded space or symbol like ? - +! @ #
% ^ & * ( ) [ ] { } . ; : " ' / and \. $ However, an underscore
( _ ) can be used.
 It should not be a reserved keyword as (as or integer, dim, for,
next, loop etc..)

COMMENT/REMARK STATEMENTS
A comment statement is added in the code to explain the code, it is
not executed
Examples
' this is a comment statement
Rem this is a comment statement

Any symbol, code or letters appear after word rem or after single
apostrophe (') is not executed

NAMING CONVENTIONS
You can use x,y as variable names, x1, x2,x3 etc..
But it is preferable to use standard naming conventions in VB.net as
follows

 It is very important to give a variable a meaningful name to


denote its meaning, for example Student variables as Name, ID,
Grade, GPA should given a name as StudentName, StudentID,
StudentGrade, StudentGPA or Name, ID, Grade, GPA but not
x1,x2,x3,x4
 If a variable name contains one word, Capitalize first letter as
Grade not grade, as Name not name
 If a variable name contains two or more words, Capitalize the
first letter of every word, don’t add spaces between words
As student name is named as StudentName
Employee salary is named as EmployeeSalary

AUTHOR: MOHAMMED ABDEL KHALIQ DWIKAT EMAIL:dwikatmo@gmail.com


TOPIC: VISUAL BASIC I / IDENTIFIERS DATE: 10/25/23 PAGE: 2 OF 11
DECLARING & USING VISUAL BASIC.NET VARIABLES AND CONSTANTS, STATIC

DECLARING VISUAL BASIC VARIABLES


Dim VariableName as DataType
For example
'Declare an integer type variable (Age)
Dim Age as integer
'Declare 2 integer type variables (Age,Grade)
Dim Age as integer, Grade as integer

Dim i, j, k As Integer
' All three variables in the preceding statement are declared as
Integer.
Dim l, m As Long, x, y As Single
' In the preceding statement, l and m are Long, x and y are
Single.
VARIABLE INITIALIZATION IN VB.NET
By default, variable value for numbers is set to zero value, for
string is set to empty value
Dim Age as integer
Age = 21
Or
Dim Age as integer = 20
Dim Age as integer = 21, Grade as integer = 92

A variable is nothing but a name given to a storage area that our


programs can manipulate. Each variable in VB.Net has a specific type,
which determines the size and layout of the variable's memory; the
range of values that can be stored within that memory; and the set of
operations that can be applied to the variable.

We will discuss basic data types. The basic value types provided in
VB.Net can be categorized as:

Type Example

Integer types SByte, Byte, Short, Integer, Long


Floating point types Single and Double
Decimal types Decimal (either integer or float)
Boolean types True or False values, as assigned
String Any symbols values
Date Any valid Date 22/12/2019

AUTHOR: MOHAMMED ABDEL KHALIQ DWIKAT EMAIL:dwikatmo@gmail.com


TOPIC: VISUAL BASIC I / IDENTIFIERS DATE: 10/25/23 PAGE: 3 OF 11
Variables are initialized (assigned a value) with an equal sign
followed by a constant expression. The general form of initialization
is:

Variable = Value

X=20

Y=Z and so

You can initialize a variable at the time of declaration as follows:

Dim x As Double
x = 5.64159

Dim StudentID As Integer = 11912457


Dim StudentName As String = "Mohammed Dwikat"
Example
Try the following example which makes use of various types of
variables:

Module variablesNdataypes
Sub Main()
Dim a As Short
Dim b As Integer
Dim c As Double
a = 10
b = 20
c = a + b
Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c)
Console.ReadLine()
End Sub
End Module

LVALUES AND RVALUES


There are two kinds of expressions:

 lvalue : An expression that is an lvalue may appear as either


the left-hand or right-hand side of an assignment.
 rvalue : An expression that is an rvalue may appear on the
right- but not left-hand side of an assignment.

AUTHOR: MOHAMMED ABDEL KHALIQ DWIKAT EMAIL:dwikatmo@gmail.com


TOPIC: VISUAL BASIC I / IDENTIFIERS DATE: 10/25/23 PAGE: 4 OF 11
Variables are lvalues and so may appear on the left-hand side of an
assignment. Numeric literals are rvalues and so may not be assigned
and cannot appear on the left-hand side. Following is a valid
statement:

Valid assignment statements


X=20
FirstName ="Mohammad"
LastName ="Dwikat"

FullName = FirstName & LastName


A=B
A= A+1

But following is not a valid statement and would generate compile-time error:
20 = X
"Mohammad" = FirstName

CONSTANTS IN VISUAL BASIC -DECLARING VISUAL BASIC CONSTANTS


The constants refer to fixed values that the program may not alter
during its execution. These fixed values are also called literals.

Constants can be of any of the basic data types like an integer


constant, a floating constant, a character constant, or a string
literal.

The constants are treated just like regular variables except that
their values cannot be modified after their definition.
DECLARING CONSTANTS
Const maxval As Long = 4999
Public Const message As String = "HELLO"
Private Const piValue As Double = 3.1415

VB.Net provides the following print and display (built-in) constants:

Constant Description

vbCrLf Carriage return/linefeed character combination.

vbTab Tab character.

AUTHOR: MOHAMMED ABDEL KHALIQ DWIKAT EMAIL:dwikatmo@gmail.com


TOPIC: VISUAL BASIC I / IDENTIFIERS DATE: 10/25/23 PAGE: 5 OF 11
DECLARING VISUAL BASIC VARIABLES
Static x as integer

STATEMENTS
A statement is a complete instruction in Visual Basic programs. It
may contain keywords, operators, variables, literal values, constants
and expressions.

Declaration statements - these are the statements where you name a


variable, constant, or procedure, and can also specify a data type.

Declaration Statements
The declaration statements are used to name and define procedures,
variables, properties, arrays, and constants. When you declare a
programming element, you can also define its data type, access level,
and scope.

The programming elements you may declare include variables,


constants, enumerations, classes, structures, modules, interfaces,
procedures, procedure parameters, function returns, external
procedure references, operators, properties, events, and delegates.

Following are the declaration statements in VB.Net:

S.N Statements and Description Example

1 Dim Statement Dim number As Integer


Dim quantity As Integer = 100
Declares and allocates storage Dim message As String =
space for one or more variables. "Hello!"

2 Const Statement Const maximum As Long = 1000


Declares and defines one or more
constants.

DIRECTIVES IN VB.NET
Option explicit on must define a variable before using it
Option explicit off use a variable without defining it
Option compare binary when comparing text capitals ≠ small letters
Option compare text when comparing text capitals = small letters

AUTHOR: MOHAMMED ABDEL KHALIQ DWIKAT EMAIL:dwikatmo@gmail.com


TOPIC: VISUAL BASIC I / IDENTIFIERS DATE: 10/25/23 PAGE: 6 OF 11
EXECUTABLE STATEMENTS
An executable statement performs an action. Statements calling a
procedure, branching to another place in the code, looping through
several statements, or evaluating an expression are executable
statements. An assignment statement is a special case of an
executable statement.

Statements Rule
1. By default, each statement must written on one line (press enter
at the end of the statement)
2. No 2 statements allowed to be written on the same line
3. However, if you need to write 2 statements at the same line, you
can do that by separating them with a colon :
e.g. a=3: b=5
4. If the statement is too long to fit in one line, you can add
underscore at the end of the statement and continue it on the
next line
e.g sum = A *2 + 3 /5 _
+2 - 4 + 6
Example
The following example demonstrates a decision making statement:

Module decisions
Sub Main()
'local variable definition '
Dim a As Integer = 10

' check the boolean condition using if statement '


If (a < 20) Then
' if condition is true then print the following '
Console.WriteLine("a is less than 20")
End If
Console.WriteLine("value of a is : {0}", a)
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the
following result:

a is less than 20;


value of a is : 10

AUTHOR: MOHAMMED ABDEL KHALIQ DWIKAT EMAIL:dwikatmo@gmail.com


TOPIC: VISUAL BASIC I / IDENTIFIERS DATE: 10/25/23 PAGE: 7 OF 11
DATA TYPES AVAILABLE IN VB.NET
VB.Net provides a wide range of data types. The following table shows
all the data types available:
Data Type Storage Value Range
Allocation
Boolean Depends on True or False
platform
Byte 1 byte=28 0 through 255 (unsigned)
SByte 1 byte=27 -128 through 127 (signed)
Short 2 bytes=216 -32,768 through 32,767 (signed)
Integer 4 bytes=232 -2,147,483,648 through 2,147,483,647
(signed)
Long 8 bytes=264 -9,223,372,036,854,775,808 through
9,223,372,036,854,775,807(signed)
Decimal 16 bytes=2128 0 through +/-
79,228,162,514,264,337,593,543,950,33
5 (+/-7.9...E+28) with no decimal
point; 0 through +/-
7.9228162514264337593543950335 with
28 places to the right of the decimal

Single 4 bytes -3.4028235E+38 through -1.401298E-45


for negative values;

1.401298E-45 through 3.4028235E+38


for positive values
Double 8 bytes -1.79769313486231570E+308 through -
4.94065645841246544E-324,
for negative values
4.94065645841246544E-324 through
1.79769313486231570E+308,
for positive values
String Depends on 0 to approximately 2 billion Unicode
platform characters

AUTHOR: MOHAMMED ABDEL KHALIQ DWIKAT EMAIL:dwikatmo@gmail.com


TOPIC: VISUAL BASIC I / IDENTIFIERS DATE: 10/25/23 PAGE: 8 OF 11
USING VISUAL STUDIO 2010
Run the program, from File select New Project, screen appears

Ensure to have Visual Basic on the left side column; ensure to select
Console application in the middle column.
the following screen appears

You can write code between sub main and end sub
To run the code press F5

AUTHOR: MOHAMMED ABDEL KHALIQ DWIKAT EMAIL:dwikatmo@gmail.com


TOPIC: VISUAL BASIC I / IDENTIFIERS DATE: 10/25/23 PAGE: 9 OF 11
HOW TO PRINT ON SCREEN

Console.write(): takes a parameter between parentheses to print


It will print and stay on the same line

Examples
Console.Write(5) will print number 5

Console.write(1)
Console.write(3)
Console.write(5)
Will print 135
Console.writeLine(): will print the parameter and goes to new line
Console.writeLine(1)
Console.writeLine(3)
Console.writeLine(5)

Will print
1
3
5

It is possible to print string/text


Console.write("hello") will print hello

AUTHOR: MOHAMMED ABDEL KHALIQ DWIKAT EMAIL:dwikatmo@gmail.com


TOPIC: VISUAL BASIC I / IDENTIFIERS DATE: 10/25/23 PAGE: 10 OF 11
What is the output of the following statements?
Dim a as string = "Good", b as string = "Day"
Dim x as integer =7,y as integer = 3

Vb.net statement output Notes


Console.writeLine() New Empty line
Console.Write() Error is raised
Console.write(a) Good
Console.write(a & b) GoodDay
Console.write(a & " " & b) Good Day
Console.write(x & y) 73
Console.write(x + y) 10
Console.write(a + x) Error is raised
Console.write(a & x) Good7
Console.write(a + b) GoodDay
Console.write(a + " " + b) Good Day

What is the statement that outputs: x + y = 10


Console.write("x + y = " & x + y)
What is the statement that outputs: 7 + 3 = 10
Console.write(x & "+" y & " = " & x + y)

AUTHOR: MOHAMMED ABDEL KHALIQ DWIKAT EMAIL:dwikatmo@gmail.com


TOPIC: VISUAL BASIC I / IDENTIFIERS DATE: 10/25/23 PAGE: 11 OF 11

You might also like