Working with Procedure :-- Dividing your code into procedure allows you to break it up
into more modular units. As your program become longer that’s invaluable as it stops
everything from becoming too cultured .
In visual basic , all executable code must be in procedure There are two typed of procedure
1) sub procedure (sub routine)
2) Functions Procedure: A procedure is a set of one or more program statements that can be
run or call by referring to the procedure name.
We can divide the big program into small procedures.
Due to procedure the application is easier to debug and easier to find error.
The main advantage of procedure is its reusability.
Procedures are categorized into two categories: 1) In-built procedure 2) User-defined
procedure
There are 4 types of procedure in [Link].
1) Sub procedure: It does not return value.
2) Event handling procedure: These are Sub procedures that execute in response to an event
triggered by user action.
3) Function procedures: they return a value.
4) Property procedures: used in object oriented concept.
Sub Procedure: Also known as Sub Routine.
Sub procedure may or may not have arguments. Arguments are not compulsory.
A sub procedure does not return the value.
By Val is by default argument type.
To declare procedure Sub keyword is used.
Sub Statement: Declares the name, parameters, and code that define a Sub procedure.
Syntax: [Private | Public] Sub name [(parameterlist)]
[statements]
[Exit Sub]
[statements]
End Sub
name : It specifies name of the procedure.
parameterlist : It is optional. It specifies the list of local variable names representing the
parameters of this procedure. Specifies the parameters a procedure expects when it is called.
Multiple parameters are separated by commas. The following is the syntax for one
parameter.
[optional] [ByVal | ByRef] varname[( )] [As type] [= defaultvalue]
Optional: Optional. Specifies that this parameter is not required when the procedure is
called.
ByVal : It is Optional. Specifies that the procedure cannot replace or reassign the variable
element underlying the corresponding argument in the calling code.
ByRef : It is Optional. Specifies that the procedure can modify the underlying variable element
in the calling code the same way the calling code itself can.
parametername : It is Required. Name of the local variable representing the parameter.
parametertype : Required if Option Strict is On. Data type of the local variable representing
the parameter.
defaultvalue : Required for Optional parameters. Any constant or constant expression that
evaluates to the data type of the parameter. If the type is Object, or a class, interface, array, or
structure, the default value can only be Nothing.
Example: Dim ans As Double
Private Sub sMsg()
MsgBox("Hello")
End Sub
Private Sub sum1(ByRef n1 As Integer)
MsgBox(n1 + 20)
End Sub
Private Sub butOk_Click(ByVal sender As [Link], ByVal e As [Link]) Handles
[Link]
Call sMsg()
Call sum1(Val([Link]))
End Sub
Function Statement: Declares the name, parameters, and code that define a Function
procedure.
Syntax: [Public | Private] Function name [(arglist)] [As returntype]
[statements]
[name = expression]
[Exit Function]
[statements]
[name = expression]
End Function
name : Required. Name of the procedure.
Parameterlist: Optional. List of local variable names representing the parameters of this
procedure.
returntype : Required if Option Strict is On. Data type of the value returned by this procedure.
Specifies the parameters a procedure expects when it is called. Multiple parameters are
separated by commas.
The following is the syntax for one parameter.
[ Optional ] [{ ByVal | ByRef }] parametername[( )] [ As parametertype ] [ = defaultvalue ]
Optional : Optional. Specifies that this parameter is not required when the procedure is
called.
ByVal : Optional. Specifies that the procedure cannot replace or reassign the variable
element underlying the corresponding argument in the calling code.
ByRef : Optional. Specifies that the procedure can modify the underlying variable element in
the calling code the same way the calling code itself can.
parametername : Required. Name of the local variable representing the parameter.
parametertype : Required if Option Strict is On. Data type of the local variable representing
the parameter.
defaultvalue : Required for Optional parameters. Any constant or constant expression that
evaluates to the data type of the parameter. If the type is Object, or a class, interface, array, or
structure, the default value can only be Nothing.
Example: Private Function res1(ByVal n1 As Double) As Double
Return (n1 * 3.14)
End Function
Private Sub butOk_Click(ByVal sender As [Link], ByVal e As [Link]) Handles
[Link]
ans = res1(Val([Link]))
MsgBox("Result is:" & ans)
End Sub
About SDI Most applications in Windows 95 or later use a Single Document Interface. Each
window of the application holds a single document, so if the user wants to open more
documents with that application, he must open a new window. also the default mode when
building an application with [Link]. An example of an SDI application is Windows Notepad.
Advantages of SDI An SDI interface works very well with multiple monitors and multiple
virtual desktops. It also allows users to switch between multiple open documents using the
native Windows taskbar and task manager, rather than through special code that must be
written into your application.
About MDI Multiple Document Interfaces were more popular in versions of Windows prior to
Windows 95. With an MDI, each window within an application holds multiple documents,
usually in sub-windows. Each time the user wants to open a new document, rather than
opening a new window, the document opens within the existing window and shares it with all
other open documents. An example of an MDI application is a tabbed Web browser like Firefox,
where users have an option to open documents in multiple tabs within the same window.
Advantages of MDI MDI applications can often handle multiple documents more readily than
SDI programs. For example, many MDI text editors allow the user to open multiple text files side
by side in the same window, making it easy to compare and look up information from a second
document while working on the first.
Error Handling
The error handling construct in Visual Studio .NET is known as structured exception handling.
The constructs used may be new to Visual Basic users, but should be familiar to users of C++ or
Java.
Structured exception handling is straightforward to implement, and the same concepts are
applicable to either [Link] or C#. Throughout this section, example code will be shown in both
languages.
VB .NET allows backward compatibility by also providing unstructured exception handling, via
the familiar On Error GoTo statement and Err object, although this model is not discussed in this
section.
Exceptions
Exceptions are used to handle error conditions in Visual Studio .NET. They provide information
about the error condition.
An exception is an instance of a class which inherits from the [Link] base class.
Many different types of exception class are provided by the .NET Framework, and it is also
possible to create your own exception classes.
Each type extends the basic functionality of the [Link] class by allowing further
access to information about the specific type of error that has occurred. An instance of an
Exception class is created and thrown when the .NET Framework encounters an error condition.
You can deal with exceptions by using the Try, Catch Finally construct.
Try, Catch, Finally
This construct allows you to catch errors that are thrown within your code. An example of this
construct is shown below. An attempt is made to rotate an envelope, which throws an error.
Try
Dim env As IEnvelope = New EnvelopeClass()
[Link](0D, 0D, 10D, 10D)
Dim trans As ITransform2D = env
[Link]([Link], 1D)
Catch ex As [Link]
[Link]("Error: " + [Link])
Finally
' Perform any tidy up code.
End Try
The Try block is placed around the code which may fail. If an error is thrown within the Try
block, the point of execution will switch to the first Catch block.
The Catch block handles a thrown error. A Catch block is executed when the Type of a thrown
error matches the Type of error specified by the Catch block. You can have more than one Catch
block to handle different kinds of errors. The code shown below checks first if the exception
thrown is a DivideByZeroException.
Try, Catch and Finally keywords.
The Try, Catch and Finally keywords are usually referred to as Error Handling. Handling
exceptions with these methods will stop your application from receiving runtime errors (errors
encountered during running the application) caused by exceptions.
When you handle these errors, [Link] gives you the ability to get a wealth of information
about the exception and ways to let your application correspond according to them. Before we
get into getting exception information, let’s talk the complete basics.
Try - The Try keyword will tell the compiler that we want to handle an area for errors. The Try
keyword must have a Catch method, and must be encapsulated in an End Try statement.
Catch - The Catch keyword tells the application what to do if an error occurs. It will stop the
application from crashing, and let the application do the rest.
Finally - The finally keyword will occur regardless if the Try method worked successfully or a
Catch method was used. The finally keyword is optional.
Here is an example of a basic Try...Catch method:
01 Try
02 ’Create a new integer variable.
03 Dim anInteger As Integer = 0
04 ' Divide zero by zero to produce a DivideByZeroException exception.
05 anInteger \= 0
06 Catch ex As Exception
07 ' Show an error telling the user an error occured.
08 [Link]("An error occured")
09 Finally
10 ' Tell the user that it has got to the Finally statement.
11 [Link]("This has finally happened. :)")
12 End Try Payal Sheth