Embed
Email

Working with Procedures on VB dot net

Document Sample
Working with Procedures on VB dot net
Description

Working with Procedures on VB dot net

Stats
views:
5
posted:
12/19/2011
language:
pages:
28
Module 4:

Working with

Procedures

Overview







Create Write

Debug

Creating Procedures

Interface Code

and Deploy





Use Visual

Studio .NET

Using Procedures

Using Predefined Functions

Access

Data Debug

and Deploy

Lesson: Creating Procedures



What Are Procedures?

How to Create Sub Procedures

How to Create Function Procedures

How to Declare Arguments in Procedures

How to Use Optional Arguments

Code Reusability

What Are Procedures?



Procedures are the executable code statements in a

program, enclosed by a declaration statement and an

End statement

Three types:

Sub procedures (including event Sub procedures)

Function procedures

Property procedures

Enable code reuse

Declared as public by default

How to Create Sub Procedures



Sub procedures perform actions but do not return a value

to the calling procedure



[accessibility] Sub subname[(argumentlist)]

[accessibility] Sub subname[(argumentlist)]

' Sub procedure statements

' Sub procedure statements

End Sub

End Sub





Example:

Private Sub AboutHelp( )

Private Sub AboutHelp( )

MessageBox.Show("MyProgram V1.0", "MyProgram Help")

MessageBox.Show("MyProgram V1.0", "MyProgram Help")

End Sub

End Sub

How to Create Function Procedures



Function procedures perform actions and can return a

value to the calling program

[accessibility] Function name[(argumentlist)] As datatype

[accessibility] Function name[(argumentlist)] As datatype

' Function statements, including optional Return

' Function statements, including optional Return

' statement

' statement

End Function

End Function





Example:

Public Function DoubleTheValue(ByVal J As Double) As _

Public Function DoubleTheValue(ByVal J As Double) As _

Double

Double

. . .

. . .

Return J*2

Return J*2

. . .

. . .

End Function

End Function

How to Declare Arguments in Procedures

Arguments are data passed to procedures

You can pass arguments ByVal or ByRef

ByVal: The procedure cannot modify the value of the

original variable

ByRef: The procedure can modify the value of the

original variable

Exception: Nonvariable elements are never modified in

calling code, even if passed by reference

ByVal is the default in Visual Basic .NET

Syntax and example:

([ByVal|ByRef] argumentname As datatype)

([ByVal|ByRef] argumentname As datatype)

(ByVal Name As String)

(ByVal Name As String)

Multimedia: Passing Arguments

How to Use Optional Arguments



Rules for declaring optional arguments:

You must specify a default value

The default value must be a constant expression

Arguments following an optional argument must also be

optional

Syntax:

(Optional [ByVal|ByRef] argumentname As datatype = defaultvalue)

(Optional [ByVal|ByRef] argumentname As datatype = defaultvalue)





Example:

Function Add (ByVal value1 As Integer, ByVal value2 As _

Function Add (ByVal value1 As Integer, ByVal value2 As _

Integer, Optional ByVal value3 As Integer = 0) As Integer

Integer, Optional ByVal value3 As Integer = 0) As Integer

Multimedia: Writing Reusable Code

Code Reusability



Use a…

Use a… For…

For… Examples

Examples

Size

Size

Structure Objects that do not need to be extended

Structure Objects that do not need to be extended Point

Point

Temperature

Temperature

Module

Module Utility functions and global data

Utility functions and global data conversion

conversion

Forms

Class

Class Extending objects or objects that need cleanup Forms

Extending objects or objects that need cleanup Button

Button



Creating a module:

[Public|Friend] Module ModuleName

[Public|Friend] Module ModuleName

. . .

. . .

End Module

End Module

Practice: Creating a Function in a Module





Open a project





Add a module to the project





Create a function in the module





Write the code for the function

Lesson: Using Procedures



How to Use Sub Procedures

How to Use Function Procedures

How to Pass Arrays to Procedures

How to Create a Sub Main

How to Use Sub Procedures





Public Sub Hello(ByVal name As String)

Public Sub Hello(ByVal name As String)

MessageBox.Show("Hello " & name)

MessageBox.Show("Hello " & name)

End Sub

End Sub







Sub Test( )

Sub Test( )

Hello("John")

Hello("John")

End Sub

End Sub

How to Use Function Procedures



Calling a function

Include the function name and arguments on the right

side of an assignment statement

Dim celsiusTemperature As Single

Dim celsiusTemperature As Single

celsiusTemperature = FtoC(80)

celsiusTemperature = FtoC(80)

Use the function name in an expression

If FtoC(userValue) < 0 Then

If FtoC(userValue) < 0 Then

...

...

End If

End If

Practice: Using the Return Value of a Function



Create the user interface



Write code for the application

How to Pass Arrays to Procedures



Pass an array as you pass other arguments:

Sub PassArray(ByVal testScores As Integer( ))

Sub PassArray(ByVal testScores As Integer( ))

...

...

End Sub

End Sub

Dim scores( ) As Integer = {80, 92, 73}

Dim scores( ) As Integer = {80, 92, 73}

PassArray(scores)

PassArray(scores)



Declare a parameter array:

Sub StudentScores(ByVal name As String, ByVal _

Sub StudentScores(ByVal name As String, ByVal _

ParamArray scores( ) As String)

ParamArray scores( ) As String)

' Statements for Sub procedure

' Statements for Sub procedure

End Sub

End Sub



Call a procedure with a parameter array:

StudentScores("Anne","10","26","32","15","22","16")

StudentScores("Anne","10","26","32","15","22","16")

How to Create a Sub Main



Sub Main: Starting point for your application

Application.Run: Starts the application

Application.Exit: Quits the application

Practice: Creating a Sub Main



Declare module-level variables



Create a Sub Main procedure and set it

as the startup object



Write code for the Selection form





Write code to quit the application





Test the application

Lesson: Using Predefined Functions



How to Use the InputBox Function

How to Use Date and Time Functions

How to Use String Functions

How to Use Format Functions

How to Use Financial Functions

How to Use the InputBox Function



Displays a prompt in a dialog box, and returns the user

input as a string

Dim FileName As String

Dim FileName As String

FileName = InputBox("Please enter file name","Search")

FileName = InputBox("Please enter file name","Search")

How to Use Date and Time Functions



Perform calculations and operations involving dates

and times

Examples:

DateAdd: Add or subtract a specific time interval from a

date

DateAdd(DateInterval.Day, 10, billDate)

DateAdd(DateInterval.Day, 10, billDate)



DateDiff: Determine how many specified time intervals

exist between two date/time values

DateDiff(DateInterval.Day, Now, secondDate)

DateDiff(DateInterval.Day, Now, secondDate)

How to Use String Functions



Extract only a certain portion of a string

Return information about a string

Display information in a particular format

Examples:

Trim

NewString = Trim(MyString)

NewString = Trim(MyString)



Len

Length = Len(customerName)

Length = Len(customerName)



Left

Microsoft.VisualBasic.Left(customerName, 5)

Microsoft.VisualBasic.Left(customerName, 5)

How to Use Format Functions



Format numbers, dates, and times according to

accepted standards

Display regional formats without re-coding for

nationalities or regions

Examples:

FormatCurrency

FormatCurrency(amountOwed, , , TriState.True,TriState.True)

FormatCurrency(amountOwed, , , TriState.True,TriState.True)



FormatDateTime

FormatDateTime(myDate, DateFormat.LongDate)

FormatDateTime(myDate, DateFormat.LongDate)

How to Use Financial Functions



Perform calculations and operations involving

finances; for example, interest rates

Examples:

Pmt

payment = Pmt(0.0083, 24, -5000, 0, DueDate.BegOfPeriod)

payment = Pmt(0.0083, 24, -5000, 0, DueDate.BegOfPeriod)



Rate

ratePerPeriod = Rate(24, 228, -5000, 0, DueDate.BegOfPeriod, _

ratePerPeriod = Rate(24, 228, -5000, 0, DueDate.BegOfPeriod, _

0.8)*100

0.8)*100

Practice: Examining Predefined Functions





Open the document “Visual Basic

Run-time Library Members”



Examine predefined functions, methods,

and properties that you can use in your

code



Answer questions about specific

functions, such as InStr, Mid, and Right

Review







Create Write

Debug

Creating Procedures

Interface Code

and Deploy





Use Visual

Studio .NET

Using Procedures

Using Predefined Functions

Access

Data Debug

and Deploy

Lab 4.1: Creating and Using Procedures



Exercise 1: Creating Functions in a Module

Exercise 2: Working with the Main Form


Related docs
Other docs by muchamad rizal...
Computer Networking Introduction
Views: 16  |  Downloads: 0
contoh judul skripsi informatika
Views: 564  |  Downloads: 1
Network Layer Computer Networking
Views: 14  |  Downloads: 0
Working with Procedures on VB dot net
Views: 5  |  Downloads: 0
Working with Forms and Controls VB dot net
Views: 5  |  Downloads: 0
Network security Computer Networking
Views: 11  |  Downloads: 0
Web Forms and XML Web Services VB dot net
Views: 4  |  Downloads: 0
Handling Errors and Exceptions Vb dot net
Views: 8  |  Downloads: 0
Decision Structures and Loops on VB dot net
Views: 22  |  Downloads: 0
Multimedia Networking Computer Networking
Views: 17  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!