Chapter 5 - VB - Net Modular Programming
Chapter 5 - VB - Net Modular Programming
NET functions & sub procedures dedicated/specific task and it’s upon the programmer to
decide when to break the program into these small
6.1 Chapter objectives
manageable modules.
By the end of this topic, learners should be able to:
Different programming languages refer to these modules
a) Demonstrate understanding what modular using different terms. In C and C++, we call this small
programming is module functions, In Java, C# we call them methods while
b) Explain the benefits associated with in Visual Basic.NET, we call them sub
modular programming procedures/functions. Ideally, each of the module is
c) Demonstrate understanding on how to define and supposed to perform a specific function hence avoiding
invoke methods in functions and sub procedures in code duplication, simplifying program maintenance and
VB.NET debugging as well as promoting program portability
d) Understand the difference between an argument
and parameters.
e) Exhibit the different parameter passing techniques In the previous sections, we have encountered a
6.2 Introduction programming approach where all the code (statements) has
been written inside a single block e.g. a certain event handler
Modular programming is an art of software engineering
block. In the next sections we will learn how to program in a
whereby programmers/software engineers break large
modular way by writing additional user defined functions.
and complex programs into manageable portions known
as modules. Each of the module will perform a
6.3 Benefits associated with modular programming multiple places in a program, we can simply create a
i. Less code duplication since each module will do a function and call it whenever required.
specific task.
6.5 Defining a Function in VB.NET
ii. It’s also easy to maintain the program since the
programmer is working with small program The syntax to define a function in VB.NET is as follows :
In VB.NET, the function is a separate group of codes that are ▪ Access_Specifier: It defines the access level of the
used to perform a specific task when the defined function is function such as public, private, or friend, Protected
called/invoked in a program. It’s possible to create more function to access the method.
than one function in a program to perform various ▪ Function_Name: The function_name indicate the
functionalities. The function is also useful to code name of the function that should be unique.
reusability by reducing the duplicity of the code. For ▪ ParameterList: It defines the list of the parameters
example, if we need to use the same functionality at to send or retrieve data from a method.
▪ Return_Type: It defines the data type of the variable End Function
that returns by the function
Dim x As Integer = txtFirst.Text or different parts of the program. It can be called with
Dim y As Integer = txtSecond.Text different parameter values (though they must be of the
Result = Sum (x, y); correct type).
The above statement calls the function sum () and passes a
copy of the value stored in the variables x and y. When the The same syntax can be used to call the Sub() function
To define a sub procedure in VB.NET, we use the following ' Statement to be executed
syntax of the Sub procedure:
End Sub
[Access_Specifier ] Sub Sub_name [ (parameterList) ]
End Sub
End Sub
Sub Mul(ByVal x As Integer, ByVal y As Integer) Dim x As Integer = txtFirst.Text
Dim y As Integer = txtSecond.Text
' Define the local variable.
Mul (x, y);
Dim mult As Integer The above statement calls the procedure Mul () and passes a
copy of the value stored in the variables x and y. When the
mult = n1 * n2
sub procedure is called/Invoked, computer memory is
MsgBox(" Multiplication of two number is : {0}", mult) allocated for the parameters, x and y, and the value passed is
Sub swap(ByVal a As Integer, ByVal b As Integer) The syntax for the passing parameter by Reference:
' Declare a temp variable Sub Sub_method( ByRef parameter_name1, ByRef Parameter_na
me2 )
Dim temp As Integer
[ Statement to be executed]
temp = a ' save the value of a to temp
End Sub
b = a ' put the value of b into a
In the above syntax, the ByRef keyword is used to pass the
b = temp ' put the value of temp into b
Sub procedure's reference parameters.
End Sub
Let's create a program to swap the values of two variables
5.8.2 Passing parameter by Reference using the ByRef keyword. We use the above example to
demonstrate the concept of calling by reference
Dim num1, num2 As Integer Dim temp As Integer
LblNumber.Text=”Before swapping the value of 'num1' is { b = temp ' put the value of temp into b
0}", num
End Sub
lblNumber2.Text=”" Before swapping the value of 'num2' is
{0}", num2
swap(num1, num2)