Assignment 3
Assignment 3
3
Name -: Aniket shrikrushna waghode
Roll No- 101
Subject -: VB.NET
9. What is a constructor?
▪ A constructor in VB.NET is defined as a procedure that has the name New
(rather than Initialize as in VB 6.0) and can accept arguments to allow clients
to pass data into the instance to assist with initialization. Constructors do not
return values and therefore are always
declared as a Sub.
• Try − A Try block identifies a block of code for which particular exceptions will be
• Catch − A program catches an exception with an exception handler at the place in a program
where you want to handle the problem. The Catch keyword indicates the catching of an
exception.
• Finally − The Finally block is used to execute a given set of statements, whether an
exception is thrown or not thrown. For example, if you open a file, it must be closed
whether an exception is raised or not.
24. Give the name of the class used to derived user defined
exception?
▪ You can also define your own exception. User-defined exception classes are derived from
the ApplicationException class.
A class definition starts with the keyword Class followed by the class name; and the class
body, ended by the End Class statement. Following is the general form of a class
definition −
Imports System
Module Module1
Class Figure
Public length As Double
Public breadth As Double
End Class
Sub Main()
Dim Rectangle As Figure = New Figure()
Dim area As Double = 0.0
Rectangle.length = 8.0
Rectangle.breadth = 7.0
area = Rectangle.length * Rectangle.breadt h
Console.WriteLine("Area of Rectangle is : {0}", area)
Console.ReadKey()
End Su b
End Modul e
2. Explain the types of property of a class with example? The
types of property of class are :
1) The ReadOnly Property
2) The WriteOnly Property
EventObject class
It is the root class from which all event state objects shall be derived. All Events are
constructed with a reference to the object, the source, that is logically deemed to be the
object upon which the Event in question initially occurred upon.This class is defined in
java.util package. Class declaration
Class methods
2
String toString()
Example :
Imports System.Console
Module Module1
Public Class demo
Public Overloads Function add(ByVal a As Integer, ByVal b As
Integer) WriteLine("You are in function add(integer, integer)")
Return a + b
End Function
Public Overloads Function add(ByVal a As Long, ByVal b As
Long) WriteLine("You are in function add(long, long)")
Return a + b
End Function
End Class
Sub Main()
Dim obj As New demo
WriteLine(obj.add(2147483640, 4))
WriteLine(obj.add(2147483648, 1))
WriteLine("press return to exit...")
Read()
End Sub
End Module
6. What is Inheritance? Explain with proper syntax and example?
▪ Inheritance is the idea that one class, called a subclass, can be based on another class,
called a base class. Inheritance provides a mechanism for creating hierarchies of
objects.
WriteLine("Hello World”
cl
lc
▪ Providesa reference to the base class from within a derived class. If you
want to call a member of the base class from within
a derived class, you can use the syntax:
MyBase.MemberName EXAMPLE :
Public Class CTestClass
...
End Class
The following code defines a class, Class1, and a derived class, Class1Derived, each of which has
an IncSalary method.
End Function
End Class
In visual basic, we are not allowed to use any access modifiers on namespaces, because
the namespaces have no access restrictions.
Only certain access modifiers are allowed to specify based on the context in which the
member declaration occurs. In case, if we didn’t mention any access modifiers during the
member declaration, then the default access modifiers will be used depending on the
member declaration context.
For example, the top-level types which are not nested in any other types can only have
Public or Friend accessibility. The default accessibility for top-level types is Friend.
11. Write a note on Polymorphism?
Polymorphism is the concept that different objects have different implementations of the
same characteristic. but the implementations of the methods could be drastically different.
Polymorphism sounds a great deal like encapsulation, but it is different.
Encapsulation has to do with hiding the internal implementation of an object.
Polymorphism has to do with multiple classes having the same interface.
Visual Basic introduced a very limited support for polymorphism in VB 4.0 through the
use of late binding. Late binding is the technology for code to determine at runtime what
properties and methods a given object provides. This allows code to create any object and
attempt to call a method or property of that object without knowing whether the object
supports the method at compile time. Given two different classes with the same properties
and methods, a variable of type object could be used to instantiate an object of either class.
This worked, but did not provide any type safety at compile time. Type safety is the
concept to ensure that a string is used as a string, an integer is used as an integer, and a
Boolean is used as a Boolean. Without type safety, potentially more errors can occur when
a user is executing a program rather than when the program is compiled. Type safety also
is one mechanism to prevent hackers from overloading code with a destructive piece of
code.
program where you want to handle the problem. The Catch keyword indicates the
catching of an exception.
▪ Finally − The Finally block is used to execute a given set of statements, whether an
exception is thrown or not thrown. For example, if you open a file, it must be closed
13. Explain the use of Try-catch block with example? Try − A Try
block identifies a block of code for which particular exceptions will be activated. It's
a program where you want to handle the problem. The Catch keyword
EXAMPLE :
Module Module1
Sub divisionFunction(ByVal n1 As Integer, ByVal n2 As Integer)
Dim answer As Integer
Try answer = n1
\ n2
Catch ex As DivideByZeroException
Console.WriteLine("Exception: {0}", ex)
Finally
Console.WriteLine("Answer is: {0}", answer)
End Try
End Sub Sub Main()
divisionFunction(4, 0)
Console.ReadKey() End
Sub
End Module
Module User_Exception
Public Class StudentIsZeroException : Inherits Exception