Chapter 3
Chapter 3
PROGRAM
DESIGN AND
CODING
DFP40233 Visual Basic Programming
COURSE LEARNING OUTCOME
CLO1:
Construct the visual basic program by using .NET frameworks in developing
windows Application. (P4,PLO3)
CLO3:
Demonstrate effective leadership with peer in developing Visual Basic programs.
(A3,PLO5)
COMMON PROGRAMMING
TECHNIQUE
Syntax
Data Types
Global, form-level and local variable
Variables
Option Explicit
Constant
Decision Making Statements
Operators
Continue and Exit Statements
Array
SYNTAX
Syntax which represent a line, is a For example:
command statement of a programmer to Dim a As String = “Hello”
the application so it can execute the Dim A As String = “Hello”
order
A and a has different reference
In VB.NET, a syntax consist of a line
without any symbol at the end of the
statement
Thus, optimizing VB.NET code is
limited
It’s a case sensitive syntax so uppercase
and lowercase character matters
DATA TYPES
Boolean (Depends on platform) Object (4 bytes [32bit], 8 bytes [64bit])
Byte (1 byte, Unsigned) Sbyte (1 byte, Signed)
Char (2 bytes, Unsigned) Short (2 bytes, Signed)
Date (8 bytes ) Single (4 bytes)
Decimal (16 bytes) String (Depends on platform)
Double (8 bytes) Uinteger (4 bytes, Unsigned)
Integer (4 bytes) Ulong (8 bytes, Unsigned)
DateTime Properties
DATE TYPES (CONT)
Public Function Add (value As TimeSpan) As DateTime Public Function AddYears (value As Integer ) As DateTime
Public Function AddDays ( value As Double) As DateTime Public Shared Function Compare (t1 As DateTime,t2 As
DateTime) As Integer
Public Function AddHours (value As Double) As DateTime Public Function CompareTo (value As DateTime) As Integer
Public Function AddMinutes (value As Double) As DateTime Public Function Equals (value As DateTime) As Boolean
Public Function AddMonths (months As Integer) As DateTime Public Shared Function Equals (t1 As DateTime, t2 As
DateTime) As Boolean
Public Function AddSeconds (value As Double) As DateTime Public Overrides Function ToString As String
DateTime Methods
DATE TYPES (CONT)
General Date, or G M, m
Long Date,Medium Date, or D R, r
Short Date, or d s
Long Time,Medium Time, orT u
Short Time or t U
f Y, y
F
g
Date/Time properties
DATE TYPES (CONT)
Date Timer
Now TimeString
TimeOfDay Today
DateAndTime Properties
DATE TYPES (CONT) DateAndTime Methods
Public Shared Function DateAdd (Interval As DateInterval, Public Shared Function Month (DateValue As DateTime) As
Number As Double, DateValue As DateTime) As DateTime Integer
Public Shared Function DateAdd (Interval As String,Number As Public Shared Function MonthName (Month As Integer,
Double,DateValue As Object ) As DateTime Abbreviate As Boolean) As String
Public Shared Function DateDiff (Interval As DateInterval, Public Shared Function Second (TimeValue As DateTime) As
Date1 As DateTime, Date2 As DateTime, DayOfWeek As Integer
FirstDayOfWeek, WeekOfYear As FirstWeekOfYear ) As Long
Public Shared Function DatePart (Interval As DateInterval, Public Overridable Function ToString As String
DateValue As DateTime, FirstDayOfWeekValue As
FirstDayOfWeek, FirstWeekOfYearValue As FirstWeekOfYear )
As Integer
Public Shared Function Day (DateValue As DateTime) As Public Shared Function Weekday (DateValue As DateTime,
Integer DayOfWeek As FirstDayOfWeek) As Integer
Public Shared Function Hour (TimeValue As DateTime) As Public Shared Function WeekdayName (Weekday As Integer,
Integer Abbreviate As Boolean, FirstDayOfWeekValue As
FirstDayOfWeek) As String
Public Shared Function Minute (TimeValue As DateTime) As Public Shared Function Year (DateValue As DateTime) As
Integer Integer
VAL, FORMAT, UCASE
Val Format UCase
Recognize numeric value in a Display numeric values in many Returns a String or character in
variable in order without different forms specified into Upper Case
encountering character and
output it as a String
1,2,3,4,5,6,7,8,9,0 and . Style:- Only lowercase will be
• General Number converted. Others stay the same
• Fixed
• Standard
• Currency
• Percent
Dim MyValue Format(n,”style argument”) ' String to convert.
MyValue = Val("2457") ' Returns 2457. MyValue Dim LowerCase As String = "Hello World
= Val(" 2 45 7") ' Returns 2457. MyValue = Format(n,”user style”) 1234“
Val("24 and 57") ' Returns 24.
' Returns "HELLO WORLD 1234".
Dim UpperCase As String =
UCase(LowerCase)
OPTION EXPLICIT
Explanation Example
Option explicit requires the programmer Dim thisVar As Integer
declares all variable needed to be used in a
source code. thisVar = 10
Any unauthorized or new variable tried to be ' The following assignment produces a
used will render error during compile time
COMPILER ERROR because ' the
It is good practice to use to control code validity. variable is not declared and Option
It must be used before any other code statements Explicit is On.
Option Strict expand this by ensuring declared thisInt = 10 ' causes ERROR
data type cannot be changed
CONSTANTS
Variable with a fixed value Const pie As Double = 3.147
The value can’t be change after being
defined
They are also called literals
Can be in any data types
Enumeration (Enum) constants is a set
of named integer constant
OPERATORS
An Operator is a symbol that perform There are 6 types of operators:-
specific mathematical and logical Arithmetic
manipulations Comparison
Logical / Bitwise
Foundation to form business logic and
Bit Shift
calculation
Assignment
Miscellaneous
ARITHMETIC OPERATOR (A =
2, B = 7)
Operator Description Example
<> Checks if the values of two operands are equal or (A <> B) is true.
not; if values are not equal, then condition
becomes true.
> Checks if the value of left operand is greater than (A > B) is not true.
the value of right operand; if yes, then condition
becomes true.
< Checks if the value of left operand is less than the (A < B) is true.
value of right operand; if yes, then condition
becomes true.
>= Checks if the value of left operand is greater than (A >= B) is not true.
or equal to the value of right operand; if yes, then
condition becomes true.
<= Checks if the value of left operand is less than or (A <= B) is true.
equal to the value of right operand; if yes, then
condition becomes true.
COMPARISON OPERATOR
(CONT)
There are several more comparison operator such as :-
Is
It compares two object reference variables and determines if two object references refer to the same object without performing
value comparisons. If object1 and object2 both refer to the exact same object instance, result is True; otherwise, result is False
IsNot
It also compares two object reference variables and determines if two object references refer to different objects. If object1 and
object2 both refer to the exact same object instance, result is False; otherwise, result is True.
Like
It compares a string against a pattern
LOGICAL / BITWISE
OPERATOR
(A = TRUE, B = FALSE)
Operator Description Example
And It is the logical as well as bitwise AND operator. If both the operands are true, then condition (A And B) is False.
becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the
expressions.
Or It is the logical as well as bitwise OR operator. If any of the two operands is true, then condition (A Or B) is True.
becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the
expressions.
Not It is the logical as well as bitwise NOT operator. Use to reverses the logical state of its operand. Not(A And B) is True.
If a condition is true, then Logical NOT operator will make false.
Xor It is the logical as well as bitwise Logical Exclusive OR operator. It returns True if both A Xor B is True.
expressions are True or both expressions are False; otherwise it returns False. This operator does
not perform short-circuiting, it always evaluates both expressions and there is no short-circuiting
counterpart of this operator.
AndAlso It is the logical AND operator. It works only on Boolean data. It performs short-circuiting. (A AndAlso B) is False.
OrElse It is the logical OR operator. It works only on Boolean data. It performs short-circuiting. (A OrElse B) is True.
And Bitwise AND Operator copies a bit to the result if it exists in (A AND B) will give 12, which is 0000
both operands. 1100
Or Binary OR Operator copies a bit if it exists in either operand. (A Or B) will give 61, which is 0011 1101
Xor Binary XOR Operator copies the bit if it is set in one operand (A Xor B) will give 49, which is 0011
but not both. 0001
Not Binary Ones Complement Operator is unary and has the effect (Not A ) will give -61, which is 1100 0011
of 'flipping' bits. in 2's complement form due to a signed
binary number.
<< Binary Left Shift Operator. The left operands value is moved A << 2 will give 240, which is 1111 0000
left by the number of bits specified by the right operand.
>> Binary Right Shift Operator. The left operands value is moved A >> 2 will give 15, which is 0000 1111
right by the number of bits specified by the right operand.
ASSIGNMENT OPERATOR
Operator Description Example
= Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C
+= Add AND assignment operator, It adds right operand to the left operand and assigns the result to left C += A is equivalent to C = C + A
operand
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assigns the result C -= A is equivalent to C = C - A
to left operand
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result C *= A is equivalent to C = C * A
to left operand
/= Divide AND assignment operator, It divides left operand with the right operand and assigns the result to C /= A is equivalent to C = C / A
left operand (floating point division)
\= Divide AND assignment operator, It divides left operand with the right operand and assigns the result to C \= A is equivalent to C = C \A
left operand (Integer division)
^= Exponentiation and assignment operator. It raises the left operand to the power of the right operand and C^=A is equivalent to C = C ^ A
assigns the result to left operand.
&= Concatenates a String expression to a String variable or property and assigns the result to the variable or Str1 &= Str2 is same as
property. Str1 = Str1 & Str2
MISCELLANEOUS OPERATORS
Operator Description Example
Await Highest
Exponentiation (^)
All comparison operators (=, <>, <, <=, >, >=, Is, IsNot, Like, TypeOf...Is)
Negation (Not)
For j = 0 To 10
Console.WriteLine("Element({0}) = {1}", j, n(j))
Next j
Console.ReadKey()
End Sub
End Module
Module arrayApl
For i = 0 To 10
Console.WriteLine(i & vbTab & marks(i))
Next i
Console.ReadKey()
End Sub
End Module
Module arrayApl
ARRAY (CONT) Sub Main()
' an array with 5 rows and 2 columns
Dim a(,) As Integer = {{0, 0}, {1, 2}, {2, 4}, {3, 6}, {4, 8}}
Multidimensional Array Dim i, j As Integer
' output each array element's value '
For i = 0 To 4
For j = 0 To 1
Console.WriteLine("a[{0},{1}] = {2}", i, j, a(i, j))
Next j
Next i
Console.ReadKey()
End Sub
End Module
Sr.No Property Name & Description
ARRAY (CONT) 1 IsFixedSize
Gets a value indicating whether the Array has a
fixed size.
Array Properties
2 IsReadOnly
Gets a value indicating whether the Array is read-
only.
3 Length
Gets a 32-bit integer that represents the total
number of elements in all the dimensions of the
Array.
4 LongLength
Gets a 64-bit integer that represents the total
number of elements in all the dimensions of the
Array.
5 Rank
Gets the rank (number of dimensions) of the Array.
Sr.No Method Name & Description
ARRAY (CONT) 1 Public Shared Sub Clear (array As Array, index As Integer, length As
Integer)
CLASS, OBJECT
Public Sub New() 'constructor
Console.WriteLine("Object is being created")
CLASS, OBJECT
Protected width As Integer
Protected height As Integer