You are on page 1of 20

GUI Application Development Using Vb.Net(22034) H.N.

Ranotkar

Practical no. 18
XI. Program Code: (Teacher must assign separate program statement to group of 3-4
student)

Write a program to demonstrate the use of constructor & destructor

1. Write a program to demonstrate the use of constructor & destructor.

A) Constructor-

Module Module1

Sub Main()

Dim obj As New syco

obj.show()

Console.ReadLine()

End Sub

Public Class syco

Public Function show()

Console.WriteLine(“This is base class”)

End Function

Sub New()

Console.WriteLine(“Constructor Executed”)

End Sub

End Class

End Module

B) Destructor-

Module Module1

Sub Main()

Dim obj As New syco

obj.show()

End Sub

End Module

Department of Computer Engineering Page 1


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

Public Class syco

Public Function show()

Console.WriteLine(“This is base Class”)

End Function

Protected Overrides Sub Finalize()

Console.WriteLine(“Destructor executing here”)

Console.ReadLine()

End Sub

End Class

XII. Results (Output of the Program)

A) Constructor–

Constructor Executed

This is base class

B) Destructor–

This is base class

Destructor executing here

XIII. Practical Related Questions

1. Find output of following

Imports System.Console

Module Module1

Sub Main()

Dim con As New Constructor(20)

WriteLine(con.ShowAge()) Read()

End Sub

End Module

Public Class Constructor

Public Age As Integer=40

Public Sub New(ByVal x As Integer)

Department of Computer Engineering Page 2


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

End Sub

Public Function ShowAge() As Integer

Return Age

End Function End Class

Find error in following

Imports System.Console

Module Module1

Sub Main()

Dim obj As New Destroy() End Sub

End Module

Public Class Destroy

Protected Overrides Finalize()

Write(“VB.NET”)

Read()

End Sub

End Class

Ans: 40

2. Find Error in following code.

Imports System.Console

Module Module1

Sub Main()

Dim obj As New Destroy()

End Sub

End Module

Public Class Destroy

Protected Overrides Finalize()

Write(“VB.NET”)

Read()

Department of Computer Engineering Page 3


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

End Sub

End Class

Ans:

Error 1 ‘Overrides’ is not valid on a member variable declaration.

Warning 2 variable ‘Finalize’ conflicts with sub ‘Finalize’ in the base class ‘Object’
and should be declared ‘Shadows’.

Error 3 Declaration expected.

Error 4 Declaration expected.

Error 5 ‘End Sub’ must be preceded by a matching ‘Sub’.

XIV. Exercise (Teacher must assign separate exercise to group of 3-4 student)

 Implement a program to display any message at run (Using a constructor).


 Implement a program to calculate area of circle using parameterized

1. Implement a program to display any message at run time.(Using Constructor).

Module Module1

Sub Main()

Dim obj As New sample

obj.display()

Console.ReadLine()

End Sub

Class sample

Public Sub New()

Console.WriteLine(“This is Constructor”)

End Sub

Sub display()

Console.WriteLine(“This is Method”)

End Sub

End Class

End Module

Department of Computer Engineering Page 4


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

Output:

2. Implement a program to calculate area of circle using parameterized constructor.

Module Module1

Sub Main()

Dim obj As New circle(2)

obj.area()

Console.ReadLine()

End Sub

Class circle

Dim p As Double = 3.14

Dim r, a As Double

Public Sub New(ByVal i As Integer)

r=i

End Sub

Sub area()

a=p*r*r

Console.WriteLine(“Area of Circle = ” & a)

End Sub

End Class

End Module

OUTPUT:

Area of Circle = 12.56

Department of Computer Engineering Page 5


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

Practical no. 19
XI. Program Code: (Teacher must assign separate program statement to group of 3-4
student)

Write a program using concept of Inheritance.

1. Write a program using concept of Inheritance.

Module Module1

Sub Main()

Dim obj As New details

obj.show()

Console.ReadLine()

End Sub

End Module

Public Class student

Public branch As String = “Computer”

End Class

Public Class details

Inherits student

Public Function show()

Console.WriteLine(“Branch = ” & branch)

Return 0

End Function

End Class

XII. Results (Output of the Program)

Branch = Computer

XIII. Practical Related Questions

Note: Below given are few sample questions for reference. Teacher must design more such
questions so as to ensure the achievement of identified CO.

1.Write an output of following

Department of Computer Engineering Page 6


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

Public class Booksale

sub New()

Console.WriteLine(“My Base Class”)

End sub

End class

Public class studentbooksale Inherits Booksale

Sub New()

MyBase.New()

Console.WriteLine(“My child Class”)

End sub

End class

Ans:

My Base Class

My Child Class

2. Find out Error in following

Public Class Person

Public FirstName As String

Public LastName As String

Public DateOfBirth As Date

Public Gender As String

Public ReadOnly Property FullName() As String

Get

Return FirstName & ” ” & LastName

End Get

End Property

End Class

Department of Computer Engineering Page 7


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

Public Class Customer=>Inherits Person

Public CustomerID As String

Public CustomerType As String

End Class

Ans:

Error1: ‘Sub Main’ was not found in ‘ConsoleApplication1.Module1’.

Warning2: Property ‘FullName’ doesn’t return a value on all code paths. A null reference
exception could occur at run time when the result is used.

Error3: Statement cannot appear within a method body. End of method assumed.

Error4: End of statement expected.

XIV. Exercise (Teacher must assign separate exercise to group of 3-4 student)

Implement a Program for inheritance where Student is Child class and faculty is Base (Take
Appropriate variables in Base and child class)

(Space for answers)

1. Implement a program for inheritance where Student is Child Class and faculty is Base
class.(Take Appropriate variable in Base and Child class)

Module Module1

Sub Main()

Dim s As New student

s.branch()

s.year()

Console.ReadLine()

End Sub

Class faculty

Dim b As String = “Computer”

Sub branch()

Console.WriteLine(“Branch = ” & b)

End Sub

Department of Computer Engineering Page 8


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

End Class

Class student

Inherits faculty

Dim y As String = “Second Year”

Sub year()

Console.WriteLine(“Year = ” & y)

End Sub

End Class

End Module

Output:

Department of Computer Engineering Page 9


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

Practical no. 20 and 21


XI. Program Code: (Teacher must assign separate program statement to group of 3-4
student)

Write a program to implement the concept of method overloading & overriding

1. Write a program to implement the concept of method overloading & overriding.

Overloading:

Module Module1

Sub Main()

Dim res As New addition

Console.WriteLine(“Overloaded Values of Class addition”)

Console.WriteLine(res.add(10))

Console.WriteLine(res.add(35, 20))

Console.ReadLine()

End Sub

End Module

Public Class addition

Public i, j As Integer

Public Function add(ByVal i As Integer) As Integer

Return i

End Function

Public Function add(ByVal i As Integer, ByVal j As Integer) As Integer

Return i + j

End Function

End Class

Overriding:

Module Module1

Sub Main()

Dim obj As New child

Department of Computer Engineering Page 10


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

Dim result As Integer

result = obj.add(10, 5)

Console.WriteLine(“Overloaded Values of Class addition”)

Console.WriteLine(“Result =” & result)

Console.ReadLine()

End Sub

End Module

Public Class parent

Public Overridable Function add(ByVal i As Integer, ByVal j As Integer)

Return (i + j)

End Function

End Class

Public Class child

Inherits parent

Public Overrides Function add(ByVal i As Integer, ByVal j As Integer)

Console.WriteLine(“Result of Addition =” & MyBase.add(12, 18))

Return (i + j)

End Function

End Class

XII. Results (Output of the Program)

Overloading:

Overloaded Values of Class addition

10

55

In the above overloading example the same function add is called to perform different
operations based on different arguments.

Overriding:

Result of Addition =30

Department of Computer Engineering Page 11


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

Overloaded Values of Class addition

Result =15

In the above overriding the parent class function add is overridden in the child class
using the MyBase.add(12, 18) statement. So first the overridden value is displayed, then the
value from child class is display.

XIII. Practical Related Questions

Note: Below given are few sample questions for reference. Teacher must design more such
questions so as to ensure the achievement of identified CO.

1.Find output of following

Imports System

Module Module1

Class overload

Dim r As Double

Public Overloads Sub area(ByVal r)

Console.Write(“Area of the Circle :”)

Console.WriteLine(1 / 3 * 3.14 * r * r * r)

End Sub

Dim length As Integer

Dim width As Integer

Public Overloads Sub area(ByVal length, ByVal width)

Console.Write(” Area of the Rectangle :”)

Console.WriteLine(length * width)

End Sub

End Class

Sub Main()

Dim r As New overload()

r.area(3.1)

r.area(4, 5)

End Sub

Department of Computer Engineering Page 12


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

End Module

2.Implement windows application for employee details using overriding methods

Answers

1. Find output of following Code.

Area of the Circle : 31.181246

Area of the Rectangle : 20

2. Implement windows application for employee details using overriding method.

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Dim a As EmpInfo = New EmpInfo()

a.name = "ABC"
a.address = "Arvi dist.wardha"
a.EmpId = 123
a.salary = 25000
a.JoinDate = "12 - 4 - 2020"

a.ShowInfo()

Department of Computer Engineering Page 13


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

End Sub
Public Class EmpPersonalDetails
Public name As String
Public address As String
Public Overridable Sub ShowInfo()
MessageBox.Show("Employee Name" & name)
MessageBox.Show("Employee Address" & address)
End Sub
End Class
Public Class EmpInfo
Inherits EmpPersonalDetails
Public EmpId As Integer
Public salary As Integer
Public JoinDate As Date
Overloads Sub ShowInfo()
MyBase.ShowInfo()
MessageBox.Show("Employee ID" & EmpId)
MessageBox.Show("Employee Salary" & salary)
MessageBox.Show("Employee Joining Date" & JoinDate)
End Sub
End Class

End Class
OUTPUT:

Department of Computer Engineering Page 14


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

XIV. Exercise (Teacher must assign separate exercise to group of 3-4 student)

 Implement a windows application for show string concatenation using overload


 Implement a windows application for show string concatenation using overload
method

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Dim a As New nameconcat
a.name("Harshal")
a.name("harshal", "Ranotkar")
End Sub
Public Class nameconcat
Dim str1 As String
Public Overloads Sub name(ByVal str1)

Department of Computer Engineering Page 15


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

MessageBox.Show("Hi " & str1)


End Sub
Dim str2, str3 As String
Public Overloads Sub name(ByVal str2, ByVal str3)
MessageBox.Show("Hi " & str2 & " " & str3)
End Sub
End Class

End Class

Output:

Department of Computer Engineering Page 16


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

Practical no. 22
XI. Program Code: (Teacher must assign separate program statement to group of 3-4
student)

1. Write a program to using shadowing in inheritance.

Module Module1

Sub Main()

Dim p As New parent

Dim c As New child

p.use()

c.use()

Console.WriteLine(“________________________________”)

p.disp()

c.disp()

Console.ReadLine()

End Sub

Public Class parent

Public Sub disp()

Console.WriteLine(“Parent”)

End Sub

Public Sub use()

Me.disp()

End Sub

End Class

Public Class child

Inherits parent

Public Shadows Sub disp()

Console.WriteLine(“Child”)

End Sub

Department of Computer Engineering Page 17


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

End Class

End Module

XII. Results (Output of the Program)

Parent

Parent

________________________________

Parent

Child

XIII. Practical Related Questions

Note: Below given are few sample questions for reference. Teacher must design more such
questions so as to ensure the achievement of identified CO.

1.Write output of following code Class Shadow

Shared x As Integer = 1

Shared Sub Main()

Dim x As Integer = 10

Console.WriteLine(“main: x” & x)

Console.WriteLine(“main sahdow x:” & Shadow.x)

End Sub

End Class

Ans:

Error 1 ‘Sub Main’ was not found in ‘ConsoleApplication22.Module1’.

2.Write output of following code Public Class Form2

Dim x As Integer = 10

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button1.Click

Dim x As Integer = 30

MsgBox(x)

End Sub

Department of Computer Engineering Page 18


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

End Class

Ans:

Error 1 : Handles clause requires a WithEvents variable defined in the containing type or
one of its base types.

XIV. Exercise (Teacher must assign separate exercise to group of 3-4 student)

 Implement the concept of shadowing through inheritance in a console

(Space for answers)

1. Implement the concept of shadowing through inheritance in a console application.

Module Module1

Dim f As New first

Dim s As New second

Dim t As New third

Sub Main()

f.display()

s.display()

t.display()

Console.ReadLine()

End Sub

End Module

Public Class first

Public Sub display()

Console.WriteLine(“This is First Year”)

End Sub

End Class

Public Class second

Inherits first

Private Shadows Sub display()

Console.WriteLine(“This is Second Year”)

Department of Computer Engineering Page 19


GUI Application Development Using Vb.Net(22034) H.N.Ranotkar

End Sub

End Class

Public Class third

Inherits second

Public Shadows Sub display()

Console.WriteLine(“This is Third Year”)

End Sub

End Class

OUTPUT:

This is First Year

This is First Year

This is Third Year

Department of Computer Engineering Page 20

You might also like