Unit 4
Unit 4
NET FRAMEWORK
UNIT-4
VB.Net - Functions
• A procedure is a group of statements that together perform a task
when called. After the procedure is executed, the control returns to
the statement calling the procedure. VB.Net has two types of
procedures −
Defining a Function
• The Function statement is used to declare the name, parameter and
the body of a function. The syntax for the Function statement is −
Function Example
Function Returning a Value
Example
OUTPUT
Create Function with parameter in VB.NET
This program shows the Function keyword. We provide an Area
Function: this Function receives a radius. It returns a Double that is the
area of a circle with that radius.
OUTPUT
Create Function without parameter in VB.NET
A Function must return a value—otherwise we must use a Subroutine.
But a Function does not need to receive a value—it can have an empty
parameter list.
OUTPUT
VB.Net - Sub Procedures
• Sub procedures are procedures that do not return any value.
Example of Sub Procedure
OUTPUT
Passing Parameters by Value
• This is the default mechanism for passing parameters to a method. In
this mechanism, when a method is called, a new storage location is
created for each value parameter. The values of the actual parameters
are copied into them. So, the changes made to the parameter inside
the method have no effect on the argument.
In VB.Net, you declare the reference parameters using the ByVal keyword.
The following example demonstrates the concept −
OUTPUT
Passing Parameters by Reference
• A reference parameter is a reference to a memory location of a
variable. When you pass parameters by reference, unlike value
parameters, a new storage location is not created for these
parameters. The reference parameters represent the same memory
location as the actual parameters that are supplied to the method.
In VB.Net, you declare the reference parameters using
the ByRef keyword. The following example demonstrates this −
OUTPUT
Difference between Sub Procedure & Function in VB.NET
Sub Procedures:
• A subprocedure is a group of VB.NET statements. It begins with a Sub
keyword and ends with End Sub keywords. A subprocedure is also
called a subroutine. It is used to execute a certain block of statements
consists the body of the procedure. It is called explicitly by its name
whenever it is required to perform a certain task. It can be called any
number of times. The subprocedure returns control to the calling
code after performing a task.
Syntax: Sub <subname> [(parameter list)]
Vb statements
End Sub
Example
OUTPUT
Function Procedures:
• A function procedure is a group of VB.NET statements. It begins with
a Function keyword and ends with an End Function keyword. It is
generally used to perform a task and return a value back to the calling
code. It may have multiple return points to the calling code. A part
from return stamens, End Function, or Exit function also returns
control to the calling procedure.
Syntax of Function Procedure:
Example
OUTPUT
Class and Object in VB.NET
• A class is a group of different data members or objects with the same
properties, processes, events of an object, and general relationships to
other member functions. Furthermore, we can say that it is like a template
or architect that tells what data and function will appear when it is
included in a class object. For example, it represents the method and
variable that will work on the object of the class.
Syantax:
[ Access_Specifier ] Class ClassName
' Data Members or Variable Declaration
' Methods Name
' Statement to be executed
End Class
Example of Class
In the below syntax, we have created a class with the name
'My_program' using the Class keyword.
Class Example
OUTPUT
Object
• An object is an instance of the class.
OUTPUT
Destructor
• A destructor is a special member Sub of a class that is executed
whenever an object of its class goes out of scope.
• A destructor has the name Finalize and it can neither return a value
nor can it take any parameters. Destructor can be very useful for
releasing resources before coming out of the program like closing
files, releasing memories, etc.
Destructor Example
OUTPUT
Properties in VB.NET
Properties: In VB.NET, a Property is similar to a Function. With a getter
and a setter, it controls access to a value. This value is called a backing
store.
With Get, a property returns a value.
With Set it stores a value.
We must have both Get and Set unless we specify ReadOnly or
WriteOnly on the property.
Properties Example
OUTPUT
VB.Net - Exception Handling
• An exception is a problem that arises during the execution of a
program. An exception is a response to an exceptional circumstance
that arises while a program is running, such as an attempt to divide
by zero.
• Exceptions provide a way to transfer control from one part of a
program to another. VB.Net exception handling is built upon four
keywords - Try, Catch, Finally and Throw.
Syntax of exception handler block
Example to Exception Handle
OUTPUT
Exception Classes in .Net Framework
• In the .Net Framework, exceptions are represented by classes. The
exception classes in .Net Framework are mainly directly or indirectly
derived from the System.Exception class. Some of the exception classes
derived from the System.Exception class are
the System.ApplicationException and System.SystemException classes.
• The System.ApplicationException class supports exceptions generated by
application programs. So the exceptions defined by the programmers
should derive from this class.
• The System.SystemException class is the base class for all predefined
system exception.
• The following table provides some of the predefined exception classes
derived from the Sytem.SystemException class −
Exception Classes in .Net Framework
Handling Exceptions
OUTPUT
Basic Controls
• VB.Net provides a huge variety of controls that help you to create rich
user interface. Functionalities of all these controls are defined in the
respective control classes. The control classes are defined in
the System.Windows.Forms namespace.
2 DoubleClick
Occurs when the control is double-clicked.
Textbox Example
VB.Net - Button Control
• The Button control represents a standard Windows button. It is
generally used to generate a Click event by providing a handler for the
Click event.
• Let's create a label by dragging a Button control from the Toolbox ad
dropping it on the form.
Properties of the Button Control
Sr. No. Property & Description
4. Location Gets or sets the coordinates of the upper-left corner of the control relative
to the upper-left corner of its container.
2. Font Gets or sets the font of the text displayed by the control.
1. Appearance Gets or sets a value determining the appearance of the radio button.
2. CheckAlign Gets or sets the location of the check box portion of the radio button.
1. BorderStyle Gets or sets the type of border drawn around the list box.
4. Text Gets or searches for the text of the currently selected item in the list box.
Listbox Example
Listbox Example
• When the user chooses a destination, the text in the second label
changes −
VB.Net - ComboBox Control
• The ComboBox control is used to display a drop-down list of various items. It is a
combination of a text box in which the user enters an item and a drop-down list
from which the user selects an item.
• Let's create a combo box by dragging a ComboBox control from the Toolbox and
dropping it on the form.
Properties of the ComboBox Control
Sr. No. Property & Description
1. AutoCompleteMode Gets or sets an option that controls how automatic completion works for
the ComboBox.
3. DropDownHeight Gets or sets the height in pixels of the drop-down portion of the ComboBox.
4. DropDownStyle Gets or sets a value specifying the style of the combo box.
5. Items Gets an object representing the collection of the items contained in this
ComboBox.
1. AllowDrop Specifies whether the picture box accepts data that a user drags on it.
3. ImageLocation Gets or sets the path or the URL for the image displayed in the control.
1. Appearance Gets or sets a value determining the appearance of the check box.
2. Checked Gets or sets a value indicating whether the check box is selected.