3 - Procedures, Functions and Structures
3 - Procedures, Functions and Structures
Worker 4 Worker 5
Sub procedure
A series of statements enclosed by the Sub and End Sub
statement
4
DECLARATION OF SUB PROCEDURES
Declaration syntax
[AccessSpecifier] Sub Identifier ([Parameter])
[Statements]
End Sub
Accessspecifier Could Be Public, Protected,
Friend, Or Private
If omitted, it is public by default
Identifier specifies the identifier of the procedure
ParameterList is a comma-separated list of
parameters
Exit sub statement can be used to exit
immediately form a sub procedure
5
DECLARATION OF SUB PROCEDURES
Declaration syntax for parameters
[ByVal/ByRef] Identifier As DataType
or
optional [ByVal/ByRef] Identifier As DataType = _ DefaultValue
8
FUNCTION PROCEDURES
Function procedure
A series of statements enclosed by the function and End
Function statement
9
DECLARATION OF FUNCTION
PROCEDURES
Declaration syntax
[AccessSpecifier] Function _
Identifier ( [ParameterList] ) [As DataType]
[Statement]
Return ReturnExpression
End Sub
AccessSpecifier could be public, protected, friend or
private
If omitted, it is public by default
Identifier specifies the identifier of the function
ParameterList is comma-separated list of parameters
Data type is the data type of ReturnExpression
10
METHODS OF MATH CLASS
Function procedure (Methods) contained in class
“Math”
Performs mathematical operations and returns a value
15
FUNCTION TO DETERMINE DATA
TYPE
Method Description
IsArray(Variable Name) Checks whether the Variable is an array
IsData(Expression) Checks whether the expression is a valid data or time value
IsNumeric(Expression) Checks whether the expression evaluates to a numeric value
IsObject(Variable Name) Checks whether the variable is an object
Is Nothing Checks whether the object is set to nothing
If objMyObject Is Nothing then ….
TypeOf Checks the type of an object variable
If TypeOf txtName is TextBox Then …
TypeName(Variable Name) Returns the data type of a non object type variable
16
DATE / TIME FUNCTIONS
When a Date type variable is declared, CLR uses
the Date Time structure, which has an extensible
list of properties and methods
Now () and Today (0 are two shared members
Ex.
datToday = Today()
Non shared members could be used with the
instance name of the Date Time structure
17
DATE / TIME FUNCTIONS
Method Description
Date Date Component
Day Integer day of month (1-31)
DayOfWeek Integer day of week (0 = Sunday)
DayOfyear Integer day of year (1-366)
Hour Integer hour (0-23)
Minute Integer minute (0-59)
Second Integer second (0-59)
Month Integer month (1 = January)
Year Year component
ToLongDateString Date formatted as long date
ToLongtimeString Date formatted as long time
ToShortdateString Date formatted as short date
ToShortTimeString Date formatted as short time
18
IN BUILT STRING FUNCTIONS
Function description Example
InStr Finds the starting position of a substring within InStr(“My mother”, “mo”) = 4
a string
LCase Converts a string lower case LCase(“UPPER Case”) = upper case
Left Finds or removes a specified number of Left(“Kelaniye”, 6) = “Kelani”
Characters from the beginning of a string
Len Gives the length of a string Len(“Hello”) = 5
LTrim Removes spaces from the beginning of a string LTrim(“Hello”) = “Hello”
Mid Finds or removes characters from a Mid(“Microsoft”,3,4) = :cros”
String
StrRevers Reverses the string strReverse(“Kelaniya”) = “ayinaleK”
Right Finds or removes a specified number of Right(“Kelaniya”, 6) = “laniya”
Characters from the end of a string
RTrim Removes spaces from the end of a string RTime(“ Hello “) = “Hello”
Str Removes the string equivalent of a number Str(12345) = “12345”
Trim Trims spaces from both the beginning and end Trim(“ Hello “) = “Hello”
of a string
UCase Converts a string to upper case UCase(“lower Case”) = “UPPER CASE”
19
RECURSIVE PROCEDURES
A procedure calls itself for a repetitive task
Ex. Calculating the Factorial Value
private Function Factorial (ByVal X As Int32) As Long
If X = 1 Then
Return 1
Else
Return X * Factorial (X - 1)
End function
21
DECLARATION OF CLASSES
Declaration syntax
[AccessSpecifier] Class identifier
[Inherits Baseclass]
[MemberVariableDeclarations]
[MemberFunctionDeclarations]
End class
24
OBJECTS VARIABLES
Members of an object can be accessed with the period character
Syntax
objectVariable.Member
To set a value to a property
objectVariable. property = Expression
To get the value in a property
objectVariable.property
To call a method
objectVariable.Method [ArgumentList]
To handle an event
26
RESOURCE MANAGEMENT WITH OBJECTS
Objects consume system resource such as
memory, file handles & database connections
CLR manages resource automatically and
usually no need to worry about releasing them
of unwanted objects
However, managing resources consumed by
objects makes programs more efficient
Assign nothing to any unwanted object to release its
resources immediately
Syntax
objectVariable.Dispose (True)
27
MODULES
Like classes, encapsulate data members and
member functions defined within
Unlike classes, modules can never be
instantiated and do not support inheritance
Public members declared in a module are
accessible from anywhere in the project without
using their fully qualified names or an Imports
statement
Known as global members
Global variables and constants declared in a
module exist throughout the life of the program
28
DECLARATION OF MODULES
Declaration syntax
[AccessSpecifier] Module Identifier
[MemberVariableDeclarations]
[MemberFunctionDeclarations]
End Module
29
SCOPE
Scope of a declared element is the region in which it is
available and can be referred without using its fully
qualified name or an imports statement
Element could be a variable, constant, procedure, class,
structure or an enumeration
Use care when declaring elements with the same
identifier but with a different scope, because doing so can
lead to unexpected results
If possible, narrowing the scope of elements when
declaring them is a good programming practice 30
BLOCK LEVEL SCOPE
A block is a set of statements terminated by an
End, Else, Loop, or Next statement
32
MODULE LEVEL SCOPE
Applies equally to modules, classes, and structures scope of
an element declared within a module is determined by the
access specifier used at the declaration
Any
REFERENCE
https://youtu.be/g9B1K1gJws8
https://youtu.be/w-Poc242WoE
https://youtu.be/w-Poc242WoE