Escolar Documentos
Profissional Documentos
Cultura Documentos
V.B-PART-III
Exception Handling
An exception is a response to an abnormal or unexpected condition occurring in a program in a program, due to which the
execution of a program may be aborted. To avoid the abrupt termination of the program, the exception is generally caught and
handled appropriately. Code is written such that in case an exception occurred, alternative action is taken instead of aborting
the program abruptly.
VB.Net used a structured mechanism to handle exceptions- the try catchend try block.
Module mtry
Sub main()
Dim a as integer=9
Dim b as integer
Dim c as integer
Try
c=a/b
System.Console.WriteLine(c)
Catch s as System.Exception
System.Console.WriteLine(Error .divide by zero)
End try
End sub
End module
When keyword
Module mtry1
Sub main()
Dim a as short
Dim b as short
Dim c as decimal
System.Console.WriteLine(Enter values of a and b)
a=System.Console.ReadLine()
b= System.Console.ReadLine()
try
c=a/b
System.Console.WriteLine(c)
Catch s as System.Exception when b=0
System.Console.WriteLine(Error..divide by zero)
Finally
System.Console.WriteLine(End of main)
End try
End sub
End module
Multiple catch block
There can be multiple catch blocks in one TryEnd Try block. The order in which these catch statements are placed is very
important. If a catch with a generic exception is placed right at the beginning followed by specific exception handlers, the specific
exception handlers will never be executed as the generic exception handler will be executed first.
Option Strict on
Module trym
Sub main()
Dim a as short
Dim b as long
a=5000
b=50000
try
a=CType(b,Short)
System.Console.WriteLine(a)
Catch s as System.OverflowException
System.Console.WriteLine(Error..value exceeds range of short)
Catch s as System.Exception
System.Console.WriteLine(Exception Occured)
System.Console.WriteLine(s)
End try
End sub
End module
Page 1
ArguS academy
Finally
V.B-PART-III
The finally Keyword is optionally used with the Try .Ent try Block when an action when an action has to be carried out regardless
of whether an exception occurred or not. If the exception occurs, the control is transferred to the catch block wherein the
statements enclosed within are executed. The control is then passed onto the finally statement.
Module trym12
Sub main()
Dim a as integer
Dim b as integer
Dim c as integer
System.Controle.WriteLine(Enter value of a and b)
a=System.Console.ReadLine()
b=System.Console.ReadLine()
try
c=a/b
System.Console.WriteLine(c)
Catch s as System.Exception
System.Console.WriteLine(Errordivide by zero)
Finally
System.Console.WriteLine(End of main)
End Try
End sub
End module
Enumeration
Enumeration are used when we need to refer to values of primitive types such as byte, short, integer and long as constants or
literals
Module m1
Enum cities as short
Ranchi
Bokaro
Dhanbad
Jamshed Pur
End Enum
Sub main()
System.Console.WriteLine(cities.Dhanbad)
End sub
End module
E.g.-ii
Enum Grade as Byte
A=100
B=80
C=70
D=60
E=40
End Enum
Module m12
Sub main()
Dim gradevalue as byte
System.Console.WriteLine(Choose one of the values: 40/60/70/80/100)
Gradevalue=System.Console.ReadLine()
Select case gradevalue
Case grade.A
System.Console.WriteLine(You got an A)
Case grade.B
System.Console.WriteLine(You got an B)
Case grade.C
System.Console.WriteLine(You got an C)
Case grade.D
System.Console.WriteLine(You got an D)
Case grade.E
Page 2
ArguS academy
V.B-PART-III
System.Console.WriteLine(You got an E)
Case Else
System.Console.WriteLine(Wrong Choice)
End select
End sub
End Module
Structure
A structure is a composite user-defined data type using which we can group data of different data types together. A structure is
useful when we wish to create a record-like structure for an entity. A structure can hold not only fields but also methods,
properties and events.
Module m12
Public structure book
Public title as string
Public author as string
Public pages as integer
Public price as single
End structure
Sub main()
Dim b as new book()
System.Console.WriteLine(Enter Book Title)
b.title=System.Console.ReadLine()
System.Console.WriteLine(Enter Author Name)
b.author=System.Console.ReadLine()
System.Console.WriteLine(Enter Page)
b.page=System.Console.ReadLine()
System.Console.WriteLine(Enter Price)
b.price=System.Console.ReadLine()
System.Console.WriteLine(Book Title &b.title)
System.Console.WriteLine(Author Name &b.author)
System.Console.WriteLine(Page &b.page)
System.Console.WriteLine(Price &b.price)
Ens sub
End module
Page 3
ArguS academy
V.B-PART-III
System.Console.WriteLine(Enter Name)
name=System.Console.ReadLine()
System.Console.WriteLine(Enter Roll No)
rollno=System.Console.ReadLine()
end sub
end structure
sub main()
dim s as stud
s.accept()
System.Console.WriteLine(The student details are)
System.Console.WriteLine(Name & s.name)
System.Console.WriteLine(Roll No &s.rollno)
End sub
End Module
Page 4
ArguS academy
V.B-PART-III
Object oriented programming are analogous to real words object and define not only the data elements but also define operation
that may be applied to the elements. An object can have attributes and operations.
Public class vehicle
Public companyname as string
Public price as integer
Public mileage as integer
End class
Public sub mian()
Dim car as new vehicle
Dim truck as new vehicle
System.Console.WriteLine(Enter car company name )
car.companyname=System.Console.readLine()
System.Console.WriteLine(Enter car price )
car.price=System.Console.readLine()
System.Console.WriteLine(Enter car mileage)
car.mileage=System.Console.readLine()
System.Console.WriteLine(Enter truck company name )
truck.companyname=System.Console.readLine()
System.Console.WriteLine(Enter truck price )
truck.price=System.Console.readLine()
System.Console.WriteLine(Enter truck mileage)
truck.mileage=System.Console.readLine()
end sub
end module
Methods
Methods govern the behaviour of the object. Methods in a class typically consist of action statements similar to a function or a
procedure and are written using a sub main(). End sub block
Module m145
Public class data1
Public a as integer
Public b as integer
Public c as integer
Public sub getdata()
System.Console.WriteLine(Enter two no)
a=System.Console.ReadLine()
b=System.Console.ReadLine()
c=a+b
end sub
end class
public sub main()
dim ob as new data1
ob.getdata()
System.Console.WriteLine(Value of A = & ob.a)
System.Console.WriteLine(value of B= & ob.b)
System.Console.WriteLine(sum of A & B = & ob.c)
End sub
End module
e. g. ii
Module m111
Public name as String
Public gender as char
Private age as integer
Public address as String
Public property agevalue() as integer
Set(ByVal value as integer)
age=value
end set
Page 5
ArguS academy
V.B-PART-III
get
return age
end get
end property
public sub input()
System.Console.WriteLine(Enter the name, gender and address)
name=System.Console.ReadLine()
gender= System.Console.ReadLine()
address= System.Console.ReadLine()
end sub
end class
public sub main()
dim student as new person
student.age=19
student.input()
System.Console.writeLine()
System.Console.writeLine(Student details are )
System.Console.writeLine()
System.Console.writeLine(Student Name & student.name)
System.Console.writeLine(Student Gender & student.gender)
System.Console.writeLine(Student Age & student.age)
System.Console.writeLine(Student Address & student.address)
End sub
End module
Constructors
In VB. Net, the lifetime of an object begins when it is created with the New keywords. Constructors in VB. Net are written using
the sub New() end Sub block
Module mcon
Public class point
Public x as integer
Public y as integer
Public sub New()
System.Console.WriteLine(This is a Constructor)
System.Console.WriteLine(Enter values for point)
x= System.Console.ReadLine()
y= System.Console.ReadLine()
end sub
end class
public sub main()
dim p as new point()
System.Console.WriteLine(Point p is)
System.Console.WriteLine(Value of x= & p.x)
System.Console.WriteLine(Value of y= & p.y)
End sub
End module
Destructors
Module mdes
Public class point
Public x as integer
Public y as integer
Public sub new()
System.Console.WriteLine(This is a constructor)
System.Console.WriteLine(Enter values for point)
x= System.Console.ReadLine()
Page 6
ArguS academy
V.B-PART-III
y= System.Console.ReadLine()
end sub
overrides protected sub finalize()
System.Console.WriteLine(This is a destructor executing since object is now going to be destroyed)
End sub
End class
Public sub main()
Dim p as new point()
System.Console.WriteLine(Point p is )
System.Console.WriteLine(p.x)
System.Console.WriteLine(p.x)
End sub
End module
Page 7
ArguS academy
V.B-PART-III
End module
Property Overloading
Just as procedures and function can be overloaded, properties within a class too can be overloaded.
Module m1
Public class x
Private addr as string
Private addr2 as sting
Overloads public property address() as string
Set(Byval add as String)
addr=add
end set
get
return addr
end get
end property
overloads public property Address (s as string) as string
set (Byval add as string)
addr2=add
end set
get
return addr2
end get
end property
end class
public sub main()
Dim y as new x()
Dim s as String
System.Console.WriteLine(Enter Company Address)
y.address()=System.Console.ReadLine()
System.Console.WriteLine(Enter residence address)
y.address(residence)=System.Console.ReadLine()
System.Console.Writeline(the residence Address is :)
System.Console.Writeline(y.address(residence))
System.Console.Writeline(the Company Address is :)
System.Console.Writeline(y.address())
End sub
End module
Inheritance
Inheritance is the process of creating classes based on existing classes. Inheritance is based on the principle of code reuse. Instead
of creating a class from scratch, we can add new methods, properties, and events to existing classes, thus saving time and effort.
The class based on which the new class is created is called base class or parent class and the class that is created is called derived
or child class.
Module m123
Public class x
Public j as integer
Page 8
ArguS academy
V.B-PART-III
Private k as integer
Protected l as integer
Friend m as integer
Public sub test()
l=100
end sub
end class
public class y
Inherits x
Public n as integer
End class
Public sub main()
Dim a as new x()
Dim b as new y()
a.j=100
a.m=100
a.test()
b.j=120
b.m=130
b.test()
b.n=150
System.Console.WriteLine(Value of j in base class & a.j)
System.Console.WriteLine(Value of m in base class & a.m)
System.Console.WriteLine(Value of j in derive class & b.j)
System.Console.WriteLine(Value of m in derive class & b.m)
System.Console.WriteLine(Value of n in derive class & b.n)
End sub
End module
Page 9
ArguS academy
V.B-PART-III
Sub main()
Dim b as new base()
System.Console.WriteLine(Now creating child class object)
Dim c as new child()
End sub
End module
Abstract Classes
An abstract class is a class that cannot be instantiated. In other words, an abstract class cannot be used to create objects. It is
generally used as a template to derive other classes. An abstract class typically has abstract methods which are only method
declaration without any implementation.
Module m1
MustInherit class absbase
Public sub test()
System.Console.WriteLine(Abstract Base Class)
End sub
MustOverride Sub AnotherTest()
End class
Class child
Inherits absbase
Overrides sub AnotherTest()
System.Console.WriteLine(Implementing abstract method)
End sub
End class
Sub main()
Dim c as new child()
c.test()
c.AnotherTest()
end sub
end molude
Final classes
Final classes in VB.Net are market with the NotInheritable keyword. Final classes cannot have derived classes but can be used to
create objects.
Module m1
NotInheritable class one
Public sub test()
End sub
Public sub test(x as integer)
System.Console.WriteLine(value is & x)
End sub
End class
Sub main()
Dim f as new one()
f.test()
f.test(100)
end sub
end module
Overriding
Overriding is the process of overwriting the methods or properties of a base class in a derived class. In case of method overriding,
the return type, name and arguments of the method in the derived class must be the same as that of the method being
overridden. When an object of the derived class invokes the overridden method, it will be the derived class implementation that
will be called.
The base class method must be marked with the keyword Overridable and the derived class method must be marked with the
keyword Overrides.
Module m12
Class base
Overridable public sub test(a as integer, b as integer)
System.Console.WriteLine(Answer is & a+b)
End sub
End class
Page 10
ArguS academy
V.B-PART-III
Class child
Inherits base
Overrides public sub test(a as integer, b as integer)
System.Console.writeLine(Answer is & a*b)
End sub
End class
Sub main()
Dim b as new base()
Dim c as new child()
b.test(10,20)
c.test(10,20)
end sub
end module
Shadows keyword
If we wish to hide an inherited methods within a derived class, and invoke an overridden version of the method, then we use the
shadows keyword cannot be used with the overloads keywords.
Module m12
Class base
Public sub test()
Public sub test()
End sub
Public sub test(x as integer)
System.Console.writeLine( value from base class & x)
End sub
End class
Class child
Inherits base
Shadows sub test(y as integer)
System.Console.writeLine(value from child class & y)
End sub
End class
Sub main()
Dim c as new child()
c.test(100)
end sub
end module
MyBase Keyword
This keyword is used to refer to the base class method in a derived class that has overridden methods of the base clasystem.s
MyBase cannot act as a variable nor does it contain any value that can be passed or assigned. MyBase refer to the immediate base
class and its inherited members. It can be used to access only public and protected members of the base class.
Module m12
Public class animal
Overridable public sub eats()
System.Console.WriteLine(Eats Food)
End sub
End class
Public class cow
Inherits animal
Overrides public sub eats()
System.Console.WriteLine(Eats grass)
End sub
Public sub eating()
MyBase.eats()
System.Console.WriteLine(is a herbivorous animal )
End sub
End class
Sub main()
Dim a as new animal()
Dim c as new cow()
c.eating()
Page 11
ArguS academy
V.B-PART-III
c.eats()
end sub
end module
MyClass keyword
This keyword opposite to the way in which MyBase keyword works. Assume that a base class method is overridden in a derived
class. If the object that is used to invoke the method is of type derived class.
Module m12
Public class animal
Overridable public sub eats()
Systen.console.WriteLine(Animal eats food)
End sub
Public sub eatinghabits()
System.Console.WriteLine(Calling method directly)
Eats()
System.Console.WriteLine(Calling method using myclass)
Myclass.eats()
End sub
End class
Public class cow
Inherits Animal
Overrides public sub eats()
System.Console.WriteLine(Cow eats grass)
End sub
End class
Sub main()
Dim c as new cow()
c.eats()
c.eatinghabits()
end sub
end module
Multiple Inheritance
Module m1
Public Interface I
Sub test()
End Interface
Page 12
ArguS academy
V.B-PART-III
Public class T
Public Sub Testing()
System.Console.WriteLine(Method Testing)
End sub
End class
Public class x
Inherits T
Implements I
Sub Test() Implements I.Test
System.Console.WriteLine(Implemented method)
End sub
End class
Public sub main()
Dim y as new x()
y.test()
y.testting()
end sub
end module
Namespaces
In VB. Net class libraries are comprised of namespaces. Namespace are used to organize nested entities such as other namespace,
classes, interfaces and so on. Namespace take care of the way in which program elements are exposed to other programs with the
help of various access modifiers. Modules and classes are generally put inside namespaces.
A namespace declaration consists of the keyword Namespace followed by a qualified identifier that servers as the namespace
name. Optional namespace member declarations as enclosed between Namespace and End Namespace keywords.
Namespace plants
public class abc
public fname as String
public lname as String
public sub print()
System.Console.WriteLine("First name " & fname)
System.Console.WriteLine("Last Name " & lname)
end sub
end class
end Namespace
save this file filename.vb
module m1
sub main()
dim s as new plants.abc
System.Console.WriteLine("Enter first name")
s.fname=System.Console.ReadLine()
System.Console.WriteLine("Enter Last name")
s.lname=System.Console.ReadLine()
s.print()
end sub
end module
Save this file test.vb
The option /t: library is include with the vbc command to compile a .vb file to a dll file.
Page 13
ArguS academy
V.B-PART-III
Example 2
Imports plants
Module m1
Sub main()
Dim s as new String
s.fname=Argus
s.fname=Academy
s.print()
end sub
end module
Collection
A collection is a term used to describe a means of grouping and managing related entities. In VB.Net, a collection is used to
manage object that are exposed by a class. The simplest way to do this is to create a public instance of type
System.Collection.ICollection- This interface provides a count() method that counts the total number of items in a collection. It
also provides additional methods for advanced operations like multithreading.
System.Collection.IEnumerable- This interface exposes an object that implements IEnumeration interface. This is used
whenever a collection contains objects.
System.Collections.IList-this interface provides list type access to a collection. It provides methods to add, remove and search
for items or objects in a collection.
Module m1
Public class book
Public title as string
Public author as string
Public sub new()
System.Console.WriteLine(Enter Book Title)
title=System.Console.ReadLine()
System.Console.WriteLine(Enter Author Name)
author=System.Console.ReadLine()
end sub
end module
public bookCollection as new Microsoft.VisualBasic.Collection
public sub main()
dim b as new book()
dim c as new book()
dim d as new book()
bookCollection.Add(b)
bookCollection.Add(c)
bookCollection.Add(d)
dim i as integer
System.Console.WriteLine()
System.Console.WriteLine(list of Book Titles: )
Syste.Console.WriteLine()
For I to bookCollection.count()
System.Console.WriteLine(bookCollection.item(i).title())
next
end sub
end module
The Code declares a class called Book having Title and Author as its data members and a construction method which reads in
values for these data members from the used.
Page 14
ArguS academy
V.B-PART-III
Module m1
Enum Color
Amber
Purple
Tan
Blue
Black
Grey
Whyte
End Enum
Public class colorCollection
Inherits System.Collections.CollectionBase
Public function add(ByVal col As Color ) As Integer
MyBase.List.Add(col)
End function
Public sub insert(ByVal I as Integer, ByVal c as Color)
List.Insert(I,c)
End sub
Public sub Remove (ByVal c as Color)
List.Remove(c)
End
Default property item(ByVal I as Integer ) As Color
Get
Return CType(MyBase.List(i),Color)
End get
Set(ByVal c as Color)
myBase.List.Item(i)=c
end set
end property
end class
sub main()
dim col as new ColorCollection()
col.Add(Color.Amber)
col.Add(Color.Purple)
col.Add(Color.Tan)
col.Add(Color.Blue)
col.Add(Color.Black)
col.Add(Color.Grey)
col.Add(Color.White)
dim shade as Color
for each Shade in col
System.Console.WriteLine(shade.ToString)
Next
Shade =col(1)
System.Console.WriteLine(shade & shade.ToString)
System.Console.WriteLine()
Try
Dim n as integer =col.count()
System.Console.WriteLine(Last item added to the collection is & col.item(n-1).ToString())
Col.add(just some text)
Catch e as System.Exception
System.Console.WriteLine(e.Message)
End try
End sub
End module
Page 15
ArguS academy
V.B-PART-III
Assignments Structure Exception Handling
1. WAP to store information of 10 employees and to display information of an employee depending upon the employee no
2. WAP to accept 5 students marks from user and print the students result using structure array inside it.
3. Write a program to accept 2 numbers from the user and calculates their product. Appropriate exception handling code
must be written to take care if the product of the two numbers is very large and does not fit into the data type.
Assignments Inheritance
1. WAP to read and display information about employees and managers. Employee is the base class and Manager is the sub
class
2. WAP to display the area of circle, area of rectangle and area of cylinder all variable will be declared in super class by using
the inheritance.
3. Write a program to declare a class student. This class should have two derived classes graduate and undergraduate. The
class student should have a method test() that should print a message This is a student . This method should be
overridden in the derived classes to print the messages this is graduate and This is undergraduate respectively . write
the code such that in the main block, first only this is a student is displayed no matter which Test() method is invoked.
4. Write a program that will create a namespace N with a class X having three integer members A,B and C. these members
are declared public. Make use of this namespace N and X in another namespace M having a class Y. this program should
assign the values 100,200 and 300 to the three integer members of class X. Finally display the sum of the three integer
variables
Page 16