Procedures and Dialogs
Procedures and Dialogs
Procedure
This refers to a self-contained group of Visual Basic instructions/statements that is made to perform a
designated task i.e. each procedure’s functionality should be limited to the performance of a single, well-defined
task. Procedures facilitate code reuse because they can be called by event handlers and other procedures
The classes supplied by the .NET Framework class library all contain procedures and they can appear in a
Class or Module block
Modularity
Effective programs include modularity. Each “module” is composed of Subroutines (Methods, Procedures and
Functions). Using modularity in programs increases: clarity, organization, manageability and reusability
Types of Procedures
There are two types of general procedures:
1. Sub procedures that perform actions, but are not associated with any specific event such as a click event
and don’t return a value to the calling procedure. The Exit Sub statement causes the sub procedure to exit
immediately
Syntax:
[Public|Private] Sub ProcedureName([arguments])
Declarations & statements
End Sub
2. Function procedures (also called user-defined functions [UDFs] or simply functions) that perform
actions, which are not associated with any specific event and also return a value. You use functions to
calculate values that are returned to the calling procedure . The Exit Function statement causes the function
to exit immediately
Syntax:
[Public|Private]Function FunctionName([arguments]) [As Return Type]
function statements
[Return expression]
End Function
This causes VB to add parentheses and the End Sub statement. You can then add your code inside the
procedure.
FullNameTextBox
FirstNameTextBox
You must specify the Datatype that the function returns – this is done with the As Datatype clause in the
function declaration.
Example
Write a complete Visual Basic program that accepts the radius of a sphere using a textbox control upon the click
of a button control. The program should then use a function procedure to calculate its volume, which is then
displayed in a textbox control. Use the formula shown below for volume calculation:
In the example above the function named ComputeVolume receives a double parameter that is passed to
the function. The value incoming to the function is stored in the parameter variable named SRadius.
Function Return
There are two ways to return a value from a function:
1. Set the function name to the value to be returned inside the function.
2. Use the optional Return statement as is done below.
Notice that the Return statement terminates execution of the method and returns the value. It can occur
anywhere in method/procure
Calling a Function
Procedures are called by name and optionally a list of arguments follows this name.
A function that returns a value must be used in on the right-hand side of an assignment statement.
The value returned is stored to the variable in the assignment statement
The parameter(s) must be passed to the function by the calling statement.
Both sub and function procedures can have zero, one, or more arguments in the parameter list. The calling and
called procedure must pass/receive the same type of data for each argument in the parameter list.
Types of Parameters
There are two types of parameters:
i). Actual Parameters: - This are substituted for the formal parameter at the time the procedure is called. Each
data type must be assignment compatible with the data type of its corresponding formal parameter.
ii). Formal Parameters: - This a list of “place marker” names used in the procedure’s declaration. It can
include the data type of the valued parameters.
Examples:
Private Function ComputeVolume(ByVal CRadius As Double) As Double
#End Region
E.g
'Project: VBUniversity (Solution)
Application Programming Notes ~ Wainaina Page 4 of 7
'Author name
'Today's Date
Option Strict On
Public Class Payroll
#Region "Module level variable and constant declarations"
'Module level variable/constant declarations
Private RetirementRateDecimal As Decimal
#End Region
Optional Arguments
The caller has the option of passing a particular argument.
Must specify a default value that is assigned to the parameter if the optional argument is not passed
All optional parameters must be placed to the right of the method’s non-optional parameters
Specified in the procedure header with the keyword Optional.
E.g.
Function Power (ByVal base As Integer, Optional exponent As Long = 2)
Function calls
Call Power() ‘Causes syntax error
Call Power(2)
Call Power(10,3)
DIALOG BOXES
1. MessageBox class and MsgBox function
Visual Studio provides both the MsgBox function and the MessageBox class for displaying text in a message
box/ dialog box (i.e. displaying output to the user). The results of the function call can be assigned to a
variable.
The MessageBox class is part of the System.Windows.Forms namespace; it takes arguments much like MsgBox,
and it is displayed by using the Show method. This dialogs are extremely handy for software-user
communication and for debugging purposes
Syntax
Public Shared Function Show( ByVal caption As String, ByVal title As String, ByVal buttons As
MessageBoxButtons, ByVal icon As MessageBoxIcon) As DialogResult
The following are enumerations that work with the MessageBox class
Enumeration Members
MessageBoxButtons OK, OKCancel, YesNo, YesNoCancel, AbortRetryIgnore
MessageBoxIcon None, Information, Error, Warning, Exclamation, Question,
Asterisk, Hand, Stop
MessageBoxDefaultButton Button1, Button2, Button3
DialogResult OK, Cancel, Yes, No, Abort, Retry, Ignore
MsgBox Function
This is a function used to display a dialog box, wait for the user to click a button and returns an integer value
indicating which button the user clicked.
Syntax: MsgBox (Prompt, Button, Title, HelpFile, Context)
Buttons: This is a number expression that is the sum of values specifying the number and type of buttons to
display, the icon style to use, the identity of the default button and the modality of the message box. If omitted,
the default value is 0. This argument is optional.
Example 2
The following is a procedure that prompts the user by displaying a message box with Yes and No buttons on
whether to exit application or not
2. InputBox Dialog
This is a named Visual Basic function that displays a standard dialog box on the screen and prompts the user for
input. The input is returned as a text string to the program. In addition to a prompt string, the InputBox function
supports other arguments that you might want to use occasionally.
Syntax
Public Shared Function InputBox (ByVal prompt As String, Optional ByVal title As String,
Optional ByVal defaultResponse As String, Optional ByVal xPos As Integer, Optional ByVal yPos As Integer)
As String
Appropriate GUI
ComputeNumbersForm
SumandAverageButton
ExitButton
Program Code
Public Class ComputeNumbersForm
End Class