Vous êtes sur la page 1sur 11

VB.

Net Array ,

Functions and
Subs
UOB
Semester Five

What is an array?
So far you've been using variables quite a lot. You've
put numbers into variables, and you've put text into
variables. But you've only done this one at a time:
you've put one number into a variable, or one string of
text. You've been doing this:
Dim MyNumber As Integer
MyNumber = 5
Or this
Dim MyText As String
MyText = "A String is really just text"
Or even this:
Dim MyNumber As Integer = 5

Cont..
So one variable was holding one piece of information.
An array is a variable that can hold more than one
piece of information at a time. The MyNumber variable
above held one number 5. If you had an array variable
called MyNumbers - plural - you could hold more than
one number at a time. You set them up like this:
Dim MyNumbers(4) As Integer
MyNumbers(0) = 1
MyNumbers(1) = 2
MyNumbers(2) = 3
MyNumbers(3) = 4
MyNumbers(4) = 5

Start a new VB project.


Add a Button to your Form.
Set the Text property of the Button to "Integer Array"
Put the following code behind your button:
Dim MyNumbers(4) As Integer
MyNumbers(0) = 1
MyNumbers(1) = 2
MyNumbers(2) = 3
MyNumbers(3) = 4
MyNumbers(4) = 5
MsgBox("First Number is: " & MyNumbers(0))
MsgBox("Second Number is: " & MyNumbers(1))
MsgBox("Third Number is: " & MyNumbers(2))
MsgBox("Fourth Number is: " & MyNumbers(3))
MsgBox("Fifth Number is: " & MyNumbers(4))

Functions and Subs


The two terms refer to segments of code that are
separate from your main code.
The difference between Functions and Subs
Functions have a return value, and Subs don't.
Functions have return values data-type when defining, subs
dont
The return value of functions must be assigned into a variable
or textbox, subs dont.

Example:
Substring () is a Function.
Datediff () is a function
dateAdd () is a function

A Sub is some code or job that you create


separately from others and execute it.
Example:
All events include (click(), double-click() ,form load() )

How to create subroutine


Private Sub checkempty()
Dim var as integer
var = 0
If Trim(TextBox1.Text) = "" Or Trim(TextBox2.Text) = "" Then
var = var + 1
MsgBox("blank textbox detected")
End If
End Sub
Explanations:
Private: means the contents of this subroutine can not be accessed
externally.
Sub: is abbreviation of subroutine and it indicates that this is
subroutine code.
CheckEmpty: is the subroutine name
NB. The task of this subroutine is to display a message if both textbox1
or two are empty.
Calling Subroutines
To call subroutine you only need to write keyword Call followed by subroutines
name

Call checkempty()

Using Parameters in subroutine


Parameters are the arguments passed through the subroutine.
Private Sub AddNumbers(first As Integer, second As Integer)
When you press your return key, VB changes the part in round
brackets to this:
(ByVal first As Integer, ByVal second As Integer)
Example
Private Sub AddNumbers(ByVal first As Integer, ByVal
second As Integer)
Dim answer As Integer
answer = first + second
MsgBox "The total is " & answer
End Sub

Call the subroutine with parameters by its name


passed by number of arguments as
Call AddNumbers(first, second)

ByVal and ByRef in VB .NET

The word ByVal is short for "By Value". What it means is that you
are passing a copy of a variable to your Subroutine. You can make
changes to the copy and the original will not be altered.
ByRef is the alternative. This is short for By Reference.
This means that you are not handing over a copy of the original
variable but pointing to the original variable. Let's see a coding
example.
Example1
Private Sub byvalcheck(ByVal number As Integer)
number = number + 90
MsgBox(number)
End Sub
Example2

Private Sub byRefcheck(ByRef number As Integer)


number = number + 90
MsgBox(number)
End Sub

How to Create a Function in VB .NET


A function is a segment of code you create yourself, and that can
be used whenever you want it. The difference is that a Function
returns a value, while a Sub doesn't.
CREATING FUNCTIONS IN VB.NET
Private Function checkemptyf() As Boolean
If Trim(TextBox1.Text) = "" Or Trim(TextBox2.Text) = "" Then
checkemptyf = True
End If
End Function
Explanations:
Function: is a keyword which indicates that this is a function
As Boolean: is the data-type of return values that means each
function has a return values
checkemptyf = True: this is the statements which returns the
functions value
Calling the function:
To call a function there must be a variable or textbox which
excepts the return values of the function like;

Dim err As Boolean


err = checkemptyf()

How to use Parameters with Functions


Private Function AddTwoNumbers(ByVal first As Integer, ByVal
second As Integer) As Integer
Dim answer As Integer
answer = first + second
AddTwoNumbers = answer
End Function
Calling function with parameters
Dim first As Integer
Dim second As Integer
Dim result As Integer
first = Val(txtNumber1.Text)
second = Val(txtNumber2.Text)
result = AddTwoNumbers(first, second)
If result = 0 Then
MsgBox("Please try again ")
Else
MsgBox("The answer is " & result)
End If

Standard Modules in VB .NET


The Subs and Functions worked perfectly well
where they were - inside the two lines "Public
Class Form1" and "End Class". If you tried to
put them on a line underneath End Class you
would get lots of blue wiggly lines under your
code.
That's because the code all belongs to Form1. But
it doesn't have to. In fact, it's better to separate
all your Functions and Subs and put them
somewhere else - in something called a Module.
We'll explore the Standard Module, and see how
to move our Functions and Subs outside of Form1.
That way, we can use them in other projects.
So start a new project. Add a button to you new
form. To add a Module to your project in version
2008, click Project from the menu bar. From the
menu, click on Add Module:

Vous aimerez peut-être aussi