Trek Innovations

Thoughts For You

Worked Out Examples Of Oops Concepts in Console Application Using VB.NET

In this article I gave the examples for all Oops concepts in console applications using VB.NET. This article is only for the beginners who want to learn .NET. And in my next article I will give the same examples using C#.

In this article you will see the examples for:

  1. Single Dimension Arrays
  2. Two Dimensional Arrays
  3. Jagged Arrays
  4. Structures
  5. Classes
  6. Method Overloading
  7. Constructors
  8. Default Constructors
  9. Shared Constructors
  10. Constructor Overloading
  11. Shared Variable
  12. Parameterised Constructors
  13. Using ME Object
  14. Inheritance
  15. Overloading Methods
  16. Method Overriding
  17. Constructor With Inheritance
  18. My Base with Constructor
  19. Abstract Class
  20. Interfaces
  21. Exception Handling

———————————————————————————

SINGLE DIMENSION ARRAYS:

——————————————————————————————–

Module Module1

Sub Main()

Dim i As Integer

Dim NUM() As Integer = {10, 20, 30, 40, 50}

Console.WriteLine(NUM.Length)

For i = 0 To NUM.Length – 1

Console.Write(NUM(i) & Constants.vbTab)

Next

End Sub

End Module

———————————————————————————————

SINGLE DIMENSIONS ARRAYS ACCEPTING VALUES FROM KEYBOARD:

———————————————————————————————

Module Module1

Sub Main()

Dim i As Integer

Dim NUM(4) As Integer

Console.WriteLine(NUM.Length)

For i = 0 To NUM.Length – 1

Console.Write(“Enter Number : “)

NUM(i) = Console.ReadLine()

Next

For i = 0 To NUM.Length – 1

Console.Write(NUM(i) & Constants.vbTab)

Next

End Sub

End Module

——————————————————————————————–

SINGLE DIMENSION ARRAYS USING REDIM AND REDIM PRESERVE:

——————————————————————————————–

Module Module1

Sub Main()

Dim i, max As Integer

Dim NUM() As Integer

Console.Write(“Enter howmany numbers you want to store : “)

max = Console.ReadLine()

ReDim NUM(max – 1)

Console.WriteLine(NUM.Length)

For i = 0 To NUM.Length – 1

Console.Write(“Enter Number : “)

NUM(i) = Console.ReadLine()

Next

For i = 0 To NUM.Length – 1

Console.Write(NUM(i) & Constants.vbTab)

Next

‘Add 2 more locations in an array

ReDim Preserve NUM(NUM.Length + 1)

Console.WriteLine()

Console.WriteLine(NUM.Length)

For i = 0 To NUM.Length – 1

Console.Write(NUM(i) & Constants.vbTab)

Next

Console.Write(“Enter Number : “)

NUM(5) = Console.ReadLine()

Console.Write(“Enter Number : “)

NUM(6) = Console.ReadLine()

For i = 0 To NUM.Length – 1

Console.Write(NUM(i) & Constants.vbTab)

Next

End Sub

End Module

———————————————————————————————

RECTANGULAR ARRAYS:

———————————————————————————————

Module Module1

Sub Main()

Dim i, j As Integer

Dim NUM(,) As Integer = {{10, 20}, {30, 40}, {50, 60}}

Console.WriteLine(NUM.Length)

For i = 0 To 2

For j = 0 To 1

Console.Write(NUM(i, j) & Constants.vbTab)

Next

Console.WriteLine()

Next

End Sub

End Module

———————————————————————————————-

RECTANGULAR ARRAYS : ASSIGINING VALUES FOR KEYBOARD

———————————————————————————————-

Module Module1

Sub Main()

Dim i, j, Array_item As Integer

Dim NUM(2, 1) As Integer

Console.WriteLine(NUM.Length)

For i = 0 To 2

For j = 0 To 1

Console.Write(“Enter Number : “)

NUM(i, j) = Console.ReadLine()

Next

Next

For Each Array_item In NUM

Console.WriteLine(Array_item)

Next

End Sub

End Module

——————————————————————————————–

JAGGED ARRAYS:

——————————————————————————————–

Module Module1

Sub Main()

Dim i, j As Integer

Dim NUM(3)() As Integer

Console.WriteLine(“No. of Rows : ” & NUM.Length)

ReDim NUM(0)(3)

ReDim NUM(1)(2)

ReDim NUM(2)(0)

ReDim NUM(3)(1)

For i = 0 To NUM.Length – 1

Console.WriteLine(“No. of Cols in Row” & i + 1 & ” : ” & NUM(i).Length)

Next

For i = 0 To NUM.Length – 1

For j = 0 To NUM(i).Length – 1

Console.Write(NUM(i)(j) & Constants.vbTab)

Next

Console.WriteLine()

Next

End Sub

End Module

——————————————————————————————–

STRUCTURES: USING PUBLIC ACCESS SPECIFIER:

——————————————————————————————–

Structure Employee

Public eno As Integer

Public ename As String

Public salary As Single

Public Sub Display()

Console.WriteLine(eno & Constants.vbTab & ename & Constants.vbTab & salary)

End Sub

End Structure

Module Module1

Sub Main()

Dim e1 As Employee

e1.eno = 10

e1.ename = “Ramana”

e1.salary = 10000

e1.Display()

Dim e2 As Employee

e2.eno = 11

e2.ename = “Ramesh”

e2.salary = 15000

e2.Display()

End Sub

End Module

———————————————————————————————

STRUCTURES : USING PRIVATE ACCESS SPECIFIER:

———————————————————————————————

Structure Employee

Private eno As Integer

Private ename As String

Private salary As Single

Public Sub SetData(ByVal en As Integer, ByVal estr As String, ByVal sal As Single)

eno = en

ename = estr

salary = sal

End Sub

Public Sub Display()

Console.WriteLine(eno & Constants.vbTab & ename & Constants.vbTab & salary)

End Sub

End Structure

Module Module1

Sub Main()

Dim e1 As Employee

e1.SetData(10, “Ramana”, 10000)

e1.Display()

Dim e2 As Employee

e2.SetData(11, “Ramesh”, 15000)

e2.Display()

End Sub

End Module

——————————————————————————————–

CLASSES:

——————————————————————————————–

Class Test

Private n1, n2 As Integer

Public Sub Accept()

Console.Write(“Enter First Number : “)

n1 = Console.ReadLine()

Console.Write(“Enter First Number : “)

n2 = Console.ReadLine()

End Sub

Public Function Addition() As Integer

Return n1 + n2

End Function

Public Function Subtract() As Integer

Subtract = n1 – n2

End Function

End Class

Module Module1

Sub Main()

Dim t1 As New Test

t1.Accept()

Console.WriteLine(“Result : ” & t1.Addition())

Console.WriteLine(“Result : ” & t1.Subtract())

Dim t2 As New Test

t2.Accept()

Console.WriteLine(“Result : ” & t2.Addition())

Console.WriteLine(“Result : ” & t2.Subtract())

End Sub

End Module

———————————————————————————————

class Test

Private num, n2 As Integer

Public Sub Accept()

Console.Write(“Enter Number : “)

num = Console.ReadLine()

End Sub

Sub Show()

Console.WriteLine(“Value of num : ” & num)

End Sub

Sub Modify()

num += 10

End Sub

End class

Module Module1

Sub Main()

Dim t1 As New Test() ‘ Creating an object and Allocating memory in run time

t1.Accept()

Dim t2 As Test ‘ Declaring an object

t2 = t1

t1.Modify()

t1.Show()

t2.Show()

End Sub

End Module

———————————————————————————————-

Structure Test

Private num, n2 As Integer

Public Sub Accept()

Console.Write(“Enter Number : “)

num = Console.ReadLine()

End Sub

Sub Show()

Console.WriteLine(“Value of num : ” & num)

End Sub

Sub Modify()

num += 10

End Sub

End Structure

Module Module1

Sub Main()

Dim t1 As New Test() ‘ Creating an object and Allocating memory in run time

t1.Accept()

Dim t2 As Test ‘ Creating an object and Allocating memory in complie time

t2 = t1

t1.Modify()

t1.Show()

t2.Show()

End Sub

End Module

——————————————————————————————-

METHOD OVERLOADING:

——————————————————————————————-

Class Test

Sub Display()

Console.WriteLine(“Hello….”)

End Sub

Sub Display(ByVal x As Integer)

Console.WriteLine(x)

End Sub

Sub Display(ByVal s As Single)

Console.WriteLine(s)

End Sub

Sub Display(ByVal x As Integer, ByVal c As Char)

Console.WriteLine(x & Constants.vbTab & c)

End Sub

End Class

Module Module1

Sub Main()

Dim t1 As New Test

t1.Display()

t1.Display(100)

t1.Display(3.14F)

t1.Display(1000, “a”)

End Sub

End Module

————————————————————————————-

Class Test

Private num As Integer

Public Sub SetData(ByVal n As Integer)

num = n

End Sub

Sub Display()

Console.WriteLine(num)

End Sub

End Class

Module Module1

Sub Main()

Dim t1 As New Test

t1.SetData(10)

t1.Display()

Dim t2 As New Test

t2.SetData(20)

t2.Display()

End Sub

End Module

—————————————————————————————–

CONSTRUCTORS:DEFAULT CONSTRUCTOR:

—————————————————————————————–

Class Test

Private num As Integer

Public Sub New()

num = 100

End Sub

Sub Display()

Console.WriteLine(num)

End Sub

End Class

Module Module1

Sub Main()

Dim t1 As New Test

t1.Display()

Dim t2 As New Test

t2.Display()

End Sub

End Module

——————————————————————————————

PARAMATRISED CONSTRUCTOR:

——————————————————————————————

Class Test

Private num As Integer

Public Sub New(ByVal n As Integer)

num = n

End Sub

Sub Display()

Console.WriteLine(num)

End Sub

End Class

Module Module1

Sub Main()

Dim t1 As New Test(100)

t1.Display()

Dim t2 As New Test(200)

t2.Display()

Dim t3 As New Test() ‘Gives an error

t3.Display()

End Sub

End Module

—————————————————————————————–

CONSTRUCTOR OVERLOADING:

—————————————————————————————–

Class Test

Private num As Integer

Public Sub New()

num = 10

End Sub

Public Sub New(ByVal n As Integer)

num = n

End Sub

Sub Display()

Console.WriteLine(num)

End Sub

End Class

Module Module1

Sub Main()

Dim t1 As New Test(100)

t1.Display()

Dim t2 As New Test(200)

t2.Display()

Dim t3 As New Test()

t3.Display()

End Sub

End Module

——————————————————————————————-

USING ME OBJECT:

——————————————————————————————

Class Test

Private num As Integer

Public Sub New(ByVal num As Integer)

Me.num = num

End Sub

Sub Display()

Console.WriteLine(Me.num)

End Sub

End Class

Module Module1

Sub Main()

Dim t1 As New Test(100)

t1.Display()

Dim t2 As New Test(200)

t2.Display()

End Sub

End Module

——————————————————————————————–

SHARED VARIABLE:

——————————————————————————————–

Class Test

Public Shared cnt As Integer

Private num As Integer

Public Sub New(ByVal n As Integer)

num = n

End Sub

Public Sub Modify()

num += 10

cnt += 1

End Sub

Sub Display()

Console.WriteLine(cnt & Constants.vbTab & num)

End Sub

End Class

Module Module1

Sub Main()

Dim t1 As New Test(100)

t1.Display()

t1.Modify()

t1.Display()

Dim t2 As New Test(200)

t2.Display()

t1.Modify()

t1.Display()

Console.WriteLine(Test.cnt)

End Sub

End Module

——————————————————————————————-

SHARED CONSTRUCTOR & SHARED SUBPROGRAM OR SHARED METHOD:

——————————————————————————————-

Class Test

Public Shared cnt As Integer

Shared Sub New()

cnt = 100

End Sub

Shared Sub Display()

Console.WriteLine(cnt)

End Sub

End Class

Module Module1

Sub Main()

Test.Display()

End Sub

End Module

——————————————————————————————-

INHERITANCE:

——————————————————————————————-

Class Rectangle

Sub Area()

Console.WriteLine(“Area…”)

End Sub

End Class

Class Circle

Inherits Rectangle

Sub Perimeter()

Console.WriteLine(“Perimeter…”)

End Sub

End Class

Module Module1

Sub Main()

Dim c1 As New Circle

c1.Perimeter()

c1.Area()

Dim r1 As New Rectangle

r1.Area()

End Sub

End Module

——————————————————————————————

OVERLOADING METHOD BETWEEN 2 CLASSES:

——————————————————————————————

Class Rectangle

Sub Area(ByVal l As Integer, ByVal b As Integer)

Console.WriteLine(“Rectangle Area… : ” & (l * b))

End Sub

End Class

Class Circle

Inherits Rectangle

Overloads Sub Area(ByVal r As Intege

Console.WriteLine(“Circle Area… : ” & (3.14 * r * r))

End Sub

Sub Perimeter(ByVal r As Integer)

Console.WriteLine(“Circle Perimeter… : ” & (2 * 3.14 * r))

End Sub

End Class

Module Module1

Sub Main()

Dim c1 As New Circle

c1.Perimeter(10)

c1.Area(10)

c1.Area(10, 20)

Dim r1 As New Rectangle

r1.Area(100, 200)

End Sub

End Module

———————————————————————————————

METHOD OVERRIDING:

———————————————————————————————

Class Rectangle

Overridable Sub Area()

Console.WriteLine(“Rectangle Area… : “)

End Sub

End Class

Class Circle

Inherits Rectangle

Overrides Sub Area()

Console.WriteLine(“Circle Area… : “)

End Sub

Sub Perimeter()

Console.WriteLine(“Circle Perimeter… : “)

End Sub

End Class

Module Module1

Sub Main()

Dim c1 As New Circle

c1.Perimeter()

c1.Area()

Dim r1 As New Rectangle

r1.Area()

End Sub

End Module

——————————————————————————————

PROTECT ACCESS SPECIFIER, SHADOW DATA:

USING MYBASE WITH SUBPROGRAMS,DATA AND CONSTRUCTOR:

——————————————————————————————

Class First

Protected x, y As Integer

Overridable Sub SetData(ByVal i As Integer, ByVal j As Integer)

x = i

y = j

End Sub

Overridable Sub Display()

Console.WriteLine(“Value of First X : ” & x)

Console.WriteLine(“Value of First Y : ” & y)

End Sub

End Class

Class Second

Inherits First

Private Shadows y As Integer

Private z As Integer

Overrides Sub SetData(ByVal i As Integer, ByVal j As Integer)

MyBase.SetData(10, 20)

y = i

z = j

End Sub

Overrides Sub Display()

MyBase.Display()

Console.WriteLine(“Value of Second Y : ” & y)

Console.WriteLine(“Value of Second Z : ” & z)

Console.WriteLine(“Value of First Y in Second : ” & MyBase.y)

End Sub

End Class

Module Module1

Sub Main()

Dim s1 As New second

s1.SetData(100, 200)

s1.Display()

Dim f1 As New First

f1.SetData(11, 22)

f1.Display()

End Sub

End Module

——————————————————————————————-

CONSTRUCTOR WITH INHERITANCE:

——————————————————————————————-

Class First

Sub New()

Console.WriteLine(“Default in First”)

End Sub

End Class

Class Second

Inherits First

Sub New()

Console.WriteLine(“Default in Second”)

End Sub

End Class

Module Module1

Sub Main()

Dim s1 As New Second

End Sub

End Module

———————————————————————————————

CONSTRUCTOR WITH INHERITANCE USING PARAMATERISED CONSTRUCTOR:

———————————————————————————————

Class First

Sub New()

Console.WriteLine(“Default in First”)

End Sub

End Class

Class Second

Inherits First

Sub New()

Console.WriteLine(“Default in Second”)

End Sub

Sub New(ByVal msg As String)

Console.WriteLine(msg)

End Sub

End Class

Module Module1

Sub Main()

Dim s2 As New Second(“Perameterised in Second”)

End Sub

End Module

——————————————————————————————–

MYBASE WITH CONSTRUCTOR:

——————————————————————————————–

Class First

Sub New()

Console.WriteLine(“Default in First”)

End Sub

Sub New(ByVal msg As String)

Console.WriteLine(msg)

End Sub

End Class

Class Second

Inherits First

Sub New()

MyBase.New(“Perameterised in First”)

Console.WriteLine(“Default in Second”)

End Sub

Sub New(ByVal msg As String)

Console.WriteLine(msg)

End Sub

End Class

Module Module1

Sub Main()

Dim s1 As New Second

Dim s2 As New Second(“Perameterised in Second”)

End Sub

End Module

——————————————————————————————–

ABSTRACT CLASS:

——————————————————————————————–

MustInherit Class Shape

Protected n1, n2 As Integer

Sub New(ByVal n1 As Integer, ByVal n2 As Integer)

Me.n1 = n1

Me.n2 = n2

End Sub

MustOverride Sub Area()

End Class

Class Rectangle

Inherits Shape

Sub New(ByVal x As Integer, ByVal y As Integer)

MyBase.New(x, y)

End Sub

Public Overrides Sub Area()

Console.WriteLine(“Rectangle Area : “(n1 * n2))

End Sub

End Class

Class Square

Inherits Shape

Sub New()

MyBase.New(11, 22)

End Sub

Public Overrides Sub Area()

Console.WriteLine(“Square Area : “((n1 * n2) / 2))

End Sub

End Class

Module Module1

Sub Main()

Dim r1 As New Rectangle(10, 20)

r1.Area()

Dim s1 As New Square

s1.Area()

End Sub

End Module

———————————————————————————————–

INTERFACES:

———————————————————————————————–

Interface ABC

Sub Addition(ByVal x As Integer, ByVal y As Integer)

Sub Subtraction(ByVal x As Integer, ByVal y As Integer)

End Interface

Interface XYZ

Sub Multiply(ByVal x As Integer, ByVal y As Integer)

Sub Division(ByVal x As Integer, ByVal y As Integer)

End Interface

Class Test

Implements ABC, XYZ

Public Sub Addition(ByVal x As Integer, ByVal y As Integer) Implements ABC.Addition

Console.WriteLine(“Result : ” & (x + y))

End Sub

Public Sub Subtraction(ByVal x As Integer, ByVal y As Integer) Implements ABC.Subtraction

Console.WriteLine(“Result : ” & (x – y))

End Sub

Public Sub Division(ByVal x As Integer, ByVal y As Integer) Implements XYZ.Division

Console.WriteLine(“Result : ” & (x / y))

End Sub

Public Sub Multiply(ByVal x As Integer, ByVal y As Integer) Implements XYZ.Multiply

Console.WriteLine(“Result : ” & (x * y))

End Sub

End Class

Module Module1

Sub Main()

Dim t1 As New Test

t1.Addition(10, 20)

t1.Subtraction(10, 5)

t1.Multiply(10, 20)

t1.Division(10, 5)

End Sub

End Module

———————————————————————————————-

ONE INTERFACE INHERITS ANOTHER INTERFACE:

———————————————————————————————-

Interface ABC

Sub Addition(ByVal x As Integer, ByVal y As Integer)

Sub Subtraction(ByVal x As Integer, ByVal y As Integer)

End Interface

Interface XYZ

Inherits ABC

Sub Multiply(ByVal x As Integer, ByVal y As Integer)

Sub Division(ByVal x As Integer, ByVal y As Integer)

End Interface

Class Test

Implements XYZ

Public Sub Addition(ByVal x As Integer, ByVal y As Integer) Implements ABC.Addition

Console.WriteLine(“Result : ” & (x + y))

End Sub

Public Sub Subtraction(ByVal x As Integer, ByVal y As Integer) Implements ABC.Subtraction

Console.WriteLine(“Result : ” & (x – y))

End Sub

Public Sub Division(ByVal x As Integer, ByVal y As Integer) Implements XYZ.Division

Console.WriteLine(“Result : ” & (x / y))

End Sub

Public Sub Multiply(ByVal x As Integer, ByVal y As Integer) Implements XYZ.Multiply

Console.WriteLine(“Result : ” & (x * y))

End Sub

End Class

Module Module1

Sub Main()

Dim t1 As New Test

t1.Addition(10, 20)

t1.Subtraction(10, 5)

t1.Multiply(10, 20)

t1.Division(10, 5)

End Sub

End Module

———————————————————————————————-

EXCEPTION HANDLING:

———————————————————————————————-

Module Module1

Sub Main()

Dim n1, n2, res As Integer

Try

Console.Write(“Enter First Number : “)

n1 = Console.ReadLine()

Console.Write(“Enter Second Number : “)

n2 = Console.ReadLine()

res = n1 \ n2

Console.WriteLine(“Result : ” & res)

Catch ex As DivideByZeroException

Console.WriteLine(“Second number cannt be zero”)

Catch ex As InvalidCastException

Console.WriteLine(ex.Message)

Catch ex As OverflowException

Console.WriteLine(ex.StackTrace)

End Try

Console.WriteLine(“Last statement…”)

End Sub

End Module

——————————————————————————————————-

Module Module1

Sub Main()

Dim n1, n2, res As Integer

Try

Console.Write(“Enter First Number : “)

n1 = Console.ReadLine()

Console.Write(“Enter Second Number : “)

n2 = Console.ReadLine()

res = n1 \ n2

Console.WriteLine(“Result : ” & res)

Catch ex As Exception

Console.WriteLine(ex.Message)

End Try

Console.WriteLine(“Last statement…”)

End Sub

End Module

——————————————————————————————————–

Module Module1

Sub Main()

Dim num1() As Integer = {10, 20, 30, 40, 50, 60, 70, 80}

Dim num2() As Integer = {2, 0, 5, 4, 0}

Dim res, i As Integer

For i = 0 To num1.Length – 1

Try

res = num1(i) \ num2(i)

Console.WriteLine(“Result : ” & res)

Catch ex As Exception

Console.WriteLine(ex.Message)

End Try

Next

End Sub

End Module

——————————————————————————————————-

Module Module1

Sub Main()

Dim num1() As Integer = {10, 20, 30, 40, 50, 60, 70, 80}

Dim num2() As Integer = {2, 0, 5, 4, 0}

Dim res, i As Integer

Try

For i = 0 To num1.Length – 1

Try

res = num1(i) \ num2(i)

Console.WriteLine(“Result : ” & res)

Catch ex As DivideByZeroException

Console.WriteLine(ex.Message)

End Try

Next

Catch ex As IndexOutOfRangeException

Console.WriteLine(ex.Message)

End Try

End Sub

End Module

———————————————————————————————

THROW EXCEPTION:

———————————————————————————————

Module Module1

Sub Main()

Dim n1, n2, res As Integer

Try

Console.Write(“Enter First Number : “)

n1 = Console.ReadLine()

Console.Write(“Enter Second Number : “)

n2 = Console.ReadLine()

If n2 = 0 Then

Throw New DivideByZeroException()

Else

res = n1 \ n2

Console.WriteLine(“Result : ” & res)

End If

Catch ex As Exception

Console.WriteLine(ex.Message)

Finally

Console.WriteLine(“Last statement…”)

End Try

End Sub

End Module

———————————————————————————————

Module Module1

Sub Main()

Dim n1, n2, res As Integer

Try

Console.Write(“Enter First Number : “)

n1 = Console.ReadLine()

Console.Write(“Enter Second Number : “)

n2 = Console.ReadLine()

If n2 = 0 Then

Throw New DivideByZeroException()

Else

res = n1 \ n2

Console.WriteLine(“Result : ” & res)

End If

Catch ex As Exception

Console.WriteLine(ex.Message)

Finally

Console.WriteLine(“Last statement…”)

End Try

End Sub

End Module

————————————————————————————————

Class NilBalanceException

Inherits Exception

Private strErrorMsg As String

Sub New()

strErrorMsg = “Cant withdraw…”

End Sub

Sub New(ByVal msg As String)

strErrorMsg = msg

End Sub

Function Display() As String

Return strErrorMsg

End Function

End Class

Module Module1

Sub Main()

Dim amt As Integer

Try

Console.Write(“Enter Amount to withdraw : “)

amt = Console.ReadLine()

If amt = 0 Then

Throw New NilBalanceException(“Error Occured…”)

Else

Console.WriteLine(“Transtation Completed….”)

End If

Catch ex As NilBalanceException

Console.WriteLine(ex.Display)

Finally

Console.WriteLine(“Last statement…”)

End Try

End Sub

End Module

——————————————————————————————–

Happy Coding…………………………………

Share This Post No comments

No comments yet. Be the first.

Leave a reply