Professional Documents
Culture Documents
25/10/2023
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
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
We will discuss basic data types. The basic value types provided in
VB.Net can be categorized as:
Type Example
Variable = Value
X=20
Y=Z and so
Dim x As Double
x = 5.64159
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
But following is not a valid statement and would generate compile-time error:
20 = X
"Mohammad" = FirstName
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
Constant Description
STATEMENTS
A statement is a complete instruction in Visual Basic programs. It
may contain keywords, operators, variables, literal values, constants
and expressions.
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.
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
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
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
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