|
Functions
Variables
Variables are used to store data. A variable has a name to which we refer and the
data type, the type of data the variable holds. You need to declare variables before
using them. Variables are declared with the Dim keyword
and Dim stands for Dimension.
Variables Sample
The following code creates two variables, adds them and stores the sum in a third
variable.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles MyBase.Load
Dim x As Integer = 10
Dim y As Integer = 20
Dim z As Integer = x + y
Response.Write("Sum is" & " " & z)
End Sub
|
Constants
When we have certain values that we frequently use while programming, we should use
Constants. A value declared as constant is of fixed value that cannot be changed once
set. Constants should be declared as Public if we want it to be accessed by all parts
of the application. We use the Const keyword to declare
a constant. The following line of code declares a constant:
Public Const Pi as Double=3.14159265
SubProcedures
Procedures are a series of statements that are executed when called. Procedures makes
us handle the code in a simple and organized fashion. Sub procedures are procedures
which do not return a value. Each time when the Sub procedure is called, the statements
within it are executed until the matching End Sub is encountered. The Page_Load
event, which is the starting point of the program itself is a sub procedure. When
the application starts execution, the control is transferred to Page_Load procedure
automatically which is called by default.
SubProcedures Sample
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles MyBase.Load
Display()
'control looks for a sub procedure named Display() and executes it
End Sub
Sub Display()
Response.Write("In Sub Procedure")
End Sub
|
Functions
Function is a Procedure which returns a value. Functions are used to evaluate data,
make calculations, or to transform data. Declaring a Function is similar to declaring
a Sub procedure but functions are declared with the Function keyword.
The following is an example on Functions. This simple application is an online loan
repayment calculator that asks the user to enter the amount he wants to borrow, the
duration of the loan and based on that information calculates his monthly repayments.
To start, drag four labels, two textboxes and a button from the toolbox on to the
Web Forms page. Open the code designer and paste the following code. Look at Live
Code demo towards the botom of this page for user interface design.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
Calc()
'when the user submits his data, control passes to Calc function
End Sub
Public Function Calc() As Long
Dim amt, temp, temp1, totinterest, mi, mitemp, tot As Double
'declaring the required variables
temp = CInt(TextBox1.Text) * 13
'multiplying the amount the user wants to borrow with 13.Assuming the
'interest rate to be 13%
totinterest = temp / 100
'calculating the interest amount for the amount entered
mitemp = CInt(TextBox2.Text) * 12
'multipling the years entered with 12 to get the number of months
mi = totinterest / mitemp
'calculating monthly interest
temp1 = CInt(TextBox2.Text) * 12
'multipling the years entered with 12 to get the number of months
amt = CInt(TextBox1.Text) / temp1
'dividing the amount borrowed with number of months it is borrowed for
tot = amt + mi
'calculating the total by adding the amount + monthly interest
Label1.Text = "Your monthly repayment is:" & " " & Int(tot)
disp()
End Function
Public Sub Disp()
Label4.Text = "Not Satisfied? Would you like to try another option?"
End Sub
|
Live Code Demo
Functions
XYZ
Loan Repayment Calculator
Enter the amount you wish to borrow
Enter your loan repayment period in years
|