Meherchilakalapudi.. writes for u….

Just another WordPress.com weblog

Archive for the ‘VB.net’ Category

About VISUAL BASIC .NET

vb.net_oops_programes

Posted by meherchilakalapudi on March 16, 2009

Program1:

 

Option Strict On

Imports System.Console

Module Module1

 

    Sub Main()

        Dim i As Integer

        Dim l As Long

        Write(“Enter i value”)

        i = Read()

        WriteLine(“i value is:” & i)

        ReadLine()

        Write(“Enter l value:”)

        l = CLng(ReadLine())

        i = CInt(l)

        WriteLine(“l value is:” & l)

        WriteLine(“i value is:” & i)

    End Sub

 

End Module

 

 

Program2:

Imports System.Console

Module Module2

    Function Add(ByVal i As Integer, ByVal j As Integer) As Integer

        Dim k As Integer

        k = i + j

        ‘Return k

        Add = k

    End Function

    Function Addition(ByVal i As Integer, ByVal j As Integer, Optional ByVal k As Integer = 100) As Integer

        Return i + k + j

    End Function

    Sub main()

        Dim a, b, c As Integer

        a = 100

        b = 5

        c = Add(a, b)

        WriteLine(“Addition is:” & c)

        WriteLine(“Addition of three variables:” & Addition(a, b))

        WriteLine(“Addition of three variables Again:” & Addition(a, b, 2))

    End Sub

End Module

 

 

Program3:

 

Imports System.Console

Module Module3

    Sub main()

        Dim i As Integer = 100

        Dim j As Integer = 100

        WriteLine(“Original values::i={0},j={1}”, i, j)

        Equal(i)

        Increase(j)

        WriteLine(“After Modification::i={0},j={1}”, i, j)

    End Sub

    Sub Increase(ByRef i As Integer)

        i += 5

    End Sub

    Sub Equal(ByVal i As Integer)

        i += 5

    End Sub

 

End Module

 

 

Program4:

 

Imports System.Console

Module Module4

    Class Person

        Public name As String

        Public age As Integer

        Public address As String

        Sub setPerson()

            Write(“Enter name:”)

            name = ReadLine()

            Write(“Enter age:”)

            age = Convert.ToInt32(ReadLine())

            Write(“Enter address:”)

            address = ReadLine()

        End Sub

        Sub Display()

            WriteLine(“name::{0,25}”, name)

            WriteLine(“age::{0,25}”, age)

            WriteLine(“Address::{0,25}”, address)

        End Sub

    End Class

    Sub main()

        Dim ramu As New Person

        ramu.setPerson()

        Clear()

        ramu.Display()

        ReadLine()

    End Sub

End Module

 

 

Program5:

 

Imports System.Console

Module Module5

    Class person

        Private name As String

        Private age As Integer

        Sub setPerson()

            Write(“Enter Name:”)

            name = ReadLine()

            Write(“Enter Age:”)

            age = CInt(ReadLine())

        End Sub

        Sub getPerson()

            WriteLine(“Name={0},age={1}”, name, age)

        End Sub

    End Class

    Class Emp

        Private eno As Integer

        Private sal As Double

        Sub setEmp(ByVal p As person)

            p.setPerson()

            Write(“Enter eno:”)

            eno = CInt(ReadLine())

            Write(“Enter Sal:”)

            sal = CDbl(ReadLine())

        End Sub

        Sub getEmp(ByVal p As person)

            p.getPerson()

            WriteLine(“eno:{0},sal:{1}”, eno, sal)

        End Sub

    End Class

    Sub main()

        Dim e As New Emp

        Dim p As New person

        e.setEmp(p)

        e.getEmp(p)

    End Sub

End Module

 

 

Program6:

 

Imports System.Console

Module Module6

    Class Test

        Public i, j As Integer

        Sub New()

            WriteLine(“This is first statement”)

            i = 999 : j = 888

        End Sub

        Sub display()

            WriteLine(“i:” & ControlChars.Tab & i)

            WriteLine(“”"j:”"” & ControlChars.Tab & j)

        End Sub

        Protected Overrides Sub finalize()

            WriteLine(“This is last called”)

        End Sub

    End Class

    Sub main()

        Dim t As New Test

        t.display()

    End Sub

End Module

 

 

Program7:

Imports System.Console

Module Module7

    Class Demo

        Public name As String

        Public qualification As String

        Sub New()

            name = “vidhya”

            qualification = “Bsc.”

        End Sub

        Sub New(ByVal name As String, ByVal qualification As String)

            MyClass.name = name

            MyClass.qualification = qualification

        End Sub

        Sub display()

            WriteLine(“name:” & name)

            WriteLine(“Qualification:” & qualification)

        End Sub

    End Class

    Sub main()

        Dim d As New Demo

        d.display()

        Dim d1 As New Demo(“Leela”, “Msc”)

        d1.display()

    End Sub

End Module

 

 

Program8:

 

Imports System.Console

Module Module8

    Class Emp

        Private ename As String

        Private eno As String

        Private sal As Double

        Sub setEmp()

            Write(“Enter ename,eno,sal in sep lines”)

            ename = ReadLine()

            eno = ReadLine()

            sal = ReadLine()

        End Sub

        Sub getEmp()

            WriteLine(“Name:” & ename)

            WriteLine(“Eno:” & eno)

            WriteLine(“sal:” & sal)

        End Sub

    End Class

    Class Executive

        Inherits Emp

        Private desg As String

        Sub setExecutive()

            Write(“Enter desg:”)

            desg = ReadLine()

        End Sub

        Sub getExecutive()

            Write(“Designation:” & desg)

        End Sub

    End Class

    Sub main()

        Dim e As New Executive

        e.setEmp()

        e.setExecutive()

        e.getEmp()

        e.getExecutive()

    End Sub

End Module

 

 

Program9:

 

Imports System.Console

Module Module9

    Class Test

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

            Return i + j

        End Function

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

            Return i + j

        End Function

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

            Return i + j + k

        End Function

        Public Sub add()

            Write(“Enter i value:”)

            Dim i As Integer = CInt(ReadLine())

            Write(“Enter j value:”)

            Dim j As Integer = CInt(ReadLine())

            WriteLine(“Addition is :” & i + j)

        End Sub

        Shared Sub main()

            Dim t As New Childclass

            WriteLine(t.add(100, 101))

            WriteLine(t.add(9.19, 1.75))

            WriteLine(t.add(1, 2, 3))

            WriteLine(t.add(“bd”, “ps”))

            t.add()

        End Sub

    End Class

    Class Childclass

        Inherits Test

        Public Overloads Function add(ByVal str1 As String, ByVal str2 As String) As String

            Return str1 + str2

        End Function

    End Class

End Module

 

 

Program10:

 

Imports System.Console

Module Module10

    Class SharedDemo

        Public Shared eno As Integer

        Public Shared ename As String

        Public sal As Double

        Public Shared Sub SetValue()

            Write(“Enter eno”)

            eno = CInt(ReadLine())

            Write(“Enter ename”)

            ename = ReadLine()

        End Sub

        Public Sub getvalues()

            WriteLine(“Eno:” & eno)

            WriteLine(“Ename:” & ename)

            WriteLine(“sal:” & sal)

        End Sub

        Shared Sub main()

            Dim s As New SharedDemo

            Write(“Enter salary:”)

            s.sal = CDbl(ReadLine())

            SharedDemo.SetValue()

            s.getvalues()

        End Sub

    End Class

End Module

 

 

Program11:

 

Imports System.Console

Module Module11

    Class RawMaterial

        Public pid As Integer

        Public proname As String

        Public price As Double

        Sub New()

            WriteLine(“Base class constructor”)

            pid = 100

            proname = “Pencil”

            price = 10.95

        End Sub

        Sub New(ByVal pid As Integer, ByVal proname As String, ByVal price As Double)

            Me.pid = pid

            Me.proname = proname

            Me.price = price

        End Sub

        Sub Catalog()

            WriteLine(“Product id:” & pid)

            WriteLine(“product name:” & proname)

            WriteLine(“Price :” & price)

        End Sub

    End Class

    Class product

        Inherits RawMaterial

        Sub New()

            WriteLine(“Child class constructor”)

        End Sub

        Sub New(ByVal pid As Integer, ByVal pro As String, ByVal price As Double)

            MyBase.New(pid, pro, price)

            WriteLine(“Argument con in child class”)

        End Sub

        Sub ProductDetails()

            MyBase.Catalog()

            price *= 1.1

            WriteLine(“Final price:” & price)

        End Sub

        Shared Sub main()

            Dim p As New product

            WriteLine()

            Dim p1 As New product(159, “Pen”, 960.35)

            p1.ProductDetails()

        End Sub

    End Class

End Module

 

 

Program12:

Imports System.Console

Module Module12

    Class Demo

        Public Const c1 As Integer = 5

        Public ReadOnly r1 As Integer = 4

        Sub New(ByVal r As Integer)

            r1 = r

        End Sub

        Shared Sub main()

            Const cc As Integer = 89

            WriteLine(“Local Constant” & cc)

            WriteLine(“Demo constant” & Demo.c1)

            Dim d As New Demo(100)

            WriteLine(“readonly variable” & d.r1)

            Dim d1 As New Demo(888)

            WriteLine(“readonly variable” & d1.r1)

        End Sub

    End Class

 

 

End Module

 

 

 

 

Program13:

 

Imports System.Console

Module Module13

    Class BaseClass

        Public name As String

        Public age As Integer

        Sub SetBase()

            Write(“Enter your name:”)

            name = ReadLine()

            Write(“Enter  Age:”)

            age = ReadLine()

        End Sub

        Sub Display()

            WriteLine(“Base Class Display”)

            WriteLine(“Name:” & name)

            WriteLine(“Age:” & age)

        End Sub

    End Class

    Class ChildClass

        Inherits BaseClass

        Public q As String

        Shadows Sub Display()

            WriteLine(“Enter your qualification”)

            q = ReadLine()

            WriteLine(“Childclass Display”)

            WriteLine(“Name:” & name)

            WriteLine(“Qualification:” & q)

        End Sub

        Shared Sub main()

            Dim c As New ChildClass

            c.SetBase()

            c.Display()

            WriteLine()

            CType(c, BaseClass).Display()

 

        End Sub

    End Class

 

End Module

 

 

Program14:

 

Imports System.Console

Module Module14

    Class BaseClass

        Public Overridable Sub MyDisplay()

            WriteLine(“Base class Display”)

        End Sub

    End Class

    Class ChildClass

        Inherits BaseClass

        Public Overrides Sub MyDisplay()

            MyBase.MyDisplay()

            WriteLine(“Childclass Display”)

        End Sub

    End Class

    Sub main()

        Dim c As New ChildClass

        c.MyDisplay()

        WriteLine()

        CType(c, BaseClass).MyDisplay()

    End Sub

End Module

 

 

Program15:

Imports System.Console

Module Module15

    Sub main()

        Dim s As New Square(4)

        Dim c As New Cube(4)

        WriteLine(“Area of the square” & s.area)

        WriteLine(“Area of the cube:” & c.area)

    End Sub

    Class PreShape

        Public s As Double

        Sub New()

            s = 10

        End Sub

        Public Overridable Function area() As Double

            Return s * s

        End Function

    End Class

    MustInherit Class Shape

        Inherits PreShape

        Public MustOverride Overrides Function area() As Double

    End Class

    Class Square

        Inherits Shape

        Public side As Double

        Sub New(ByVal side As Double)

            Me.side = side

        End Sub

        Public Overrides Function area() As Double

            Return side * side

        End Function

    End Class

    Class Cube

        Inherits Shape

        Public side As Double

        Sub New(ByVal side As Double)

            MyClass.side = side

        End Sub

 

        Public Overrides Function area() As Double

            Return 6 * side * side

        End Function

    End Class

 

End Module

 

 

Program16:

 

Option Strict Off

Imports System.Console

Module Module16

    Class Animal

        Sub Breathing()

            WriteLine(“Animal takes o2 from air”)

        End Sub

    End Class

    Class Fish

        Sub Breathing()

            WriteLine(“Fish takes o2 from water”)

        End Sub

    End Class

    Sub MyBreathing(ByVal obj As Object)

        obj.Breathing()

    End Sub

 

    Sub main()

        Dim lion As New Animal

        Dim whale As New Fish

        MyBreathing(lion)

        MyBreathing(whale)

    End Sub

 

End Module

 

 

Program17:

 

Imports System.Console

Module Module17

    Class Shape

        Public i As Integer = 100

        Overridable Sub Area()

            WriteLine(“Area of the Shape” + i)

        End Sub

    End Class

    Class Square

        Inherits Shape

        Public j As Integer = 999

        Overrides Sub Area()

            WriteLine(“Area of the square” + j)

        End Sub

    End Class

    Sub main()

        Dim shape As New Shape

        Dim square As New Square

        shape.Area()

        square.Area()

        WriteLine()

        ’square = shape  ‘it generates as runtime error

        ‘(Invalid cast Exception)

        shape = square

        shape.Area()

        ’shape.j  

        ‘not possible because it is not declare with

        ‘ in the range of base class

    End Sub

End Module

 

 

Program18:

 

Imports System.Console

Module Module18

    Private name As String

    Private qualification As String

    Property myname() As String

        Get

            Return name

        End Get

        Set(ByVal value As String)

            name = value

        End Set

    End Property

    ReadOnly Property ReadQ() As String

        Get

            Return qualification

        End Get

    End Property

    WriteOnly Property WriteQ() As String

        Set(ByVal value As String)

            qualification = value

        End Set

    End Property

End Module

Module moduledemo

    Sub main()

        WriteLine(“Enter name:”)

        Module18.myname = ReadLine()

        WriteLine(“Enter Qualification”)

        Module18.WriteQ = ReadLine()

        Clear()

        WriteLine(“Your name:” & myname)

        WriteLine(“Qualification:” & ReadQ)

    End Sub

End Module

 

 

Program19:

 

Imports System.Console

Public Module Module19

    Public Class First

        Public pub As Integer = 999

        Private pri As Integer = 111

        Protected pro As Integer = 444

        Friend fri As Integer = 333

        Protected Friend profri As Integer = 666

        Public Sub FirstDisplay()

            WriteLine(“Public :” & pub)

            WriteLine(“private:” & pri)

            WriteLine(“protected:” & pro)

            WriteLine(“friend:” & fri)

            WriteLine(“protected friend” & profri)

        End Sub

    End Class

    Public Class Second

        Inherits First

        Public Sub SecondDisplay()

            WriteLine(“Public :” & pub)

            ‘WriteLine(“private:” & pri)

            WriteLine(“protected:” & pro)

            WriteLine(“friend:” & fri)

            WriteLine(“protected friend” & profri)

        End Sub

    End Class

    Public Class Third

        Dim f As New First

        Public Sub ThirdDisplay()

            WriteLine(“Public :” & f.pub)

            ‘WriteLine(“private:” & pri)

            ‘WriteLine(“protected:” & pro)

            WriteLine(“friend:” & f.fri)

            WriteLine(“protected friend” & f.profri)

        End Sub

    End Class

End Module

 

 

Program20:

 

Imports System.Console

Module Module20

    Sub main()

        Dim p As New Patient

        WriteLine(p(1))

    End Sub

    Public Class Patient

        Dim msFirstName As String

        Dim PhysiciansList As New Collection()

        Dim miPatientID As Integer

        Public Property FirstName() As String

            Get

                Return msFirstName

            End Get

            Set(ByVal Value As String)

                msFirstName = Value

            End Set

        End Property

        Default Public ReadOnly Property Physicians _

        (ByVal iIndex As Integer) As Physician

            Get

                Return CType(PhysiciansList(iIndex), Physician)

            End Get

        End Property

        Public Function Admit() As Boolean

            ‘add patient to database, notify billing, etc.

            Return True

        End Function

        Event LabResult(ByVal LabType As String)

        Public Property PatientID() As Integer

            Get

                Return miPatientID

            End Get

            Set(ByVal Value As Integer)

                miPatientID = Value

                ‘check labs database for this patient

                ‘if there are new lab results

                RaiseEvent LabResult(“CBC”)

            End Set

        End Property

    End Class

    Public Class Physician

        Dim miPhysID As Integer

        Public Property PhysicianID() As Integer

            Get

                Return miPhysID

            End Get

            Set(ByVal Value As Integer)

                miPhysID = Value

            End Set

        End Property

        Public ReadOnly Property Age() As Single

            Get

                ‘get Date of Birth (DOB)

                ‘calculate age from DOB

                ‘return age

            End Get

        End Property

    End Class

 

End Module

 

 

Program21:

 

Imports System.Console

Module Module21

    Class hai

        Private h(100) As Integer

        Default Public Property hh(ByVal i As Int16) As Integer

            Get

                Return h(i)

            End Get

            Set(ByVal value As Integer)

                h(i) = value

            End Set

        End Property

    End Class

    Sub main()

        Dim h As New hai

        h(0) = 100

        h(1) = 200

        For i As Int16 = 0 To 5

            WriteLine(h(i))

        Next

    End Sub

End Module

 

 

Program22:

 

Imports System.Console

Module Module22

    Class DefaultSample

        Dim x(100) As Integer

        Default Public Property myproperty(ByVal i As Integer) As Integer

            Get

                Return x(i)

            End Get

            Set(ByVal value As Integer)

                x(i) = value

            End Set

        End Property

    End Class

    Sub main()

        Dim d As New DefaultSample

        d.myproperty(9) = 999

        d(1) = 5

        d(2) = 4

        d(4) = 100

        For i As Integer = 0 To 10

            WriteLine(d(i))

        Next

    End Sub

End Module

 

 

Programe23:

 

Imports System.Console

Module Module23

    Sub main()

        Dim d As New Demo

        d.setDetails()

        d.getDetails()

    End Sub

    Interface Iinf

        Sub setDetails()

        Sub getDetails()

    End Interface

    Class Demo

        Implements Iinf

        Private username, pwd As String

        Public Sub getDetails() Implements Iinf.getDetails

            WriteLine(“UserName:” & username)

            WriteLine(“Pwd:” & pwd)

        End Sub

 

        Public Sub setDetails() Implements Iinf.setDetails

            WriteLine(“Enter Username:”)

            username = ReadLine()

            WriteLine(“Enter password:”)

            pwd = ReadLine()

        End Sub

    End Class

End Module

 

 

Program24:

 

Imports System.Console

Module Module24

    Interface IPerson

        Sub SetName()

        Sub SetAge()

        Sub GetName()

        Sub GetAge()

    End Interface

    Interface IExecutive

        Sub SetName()

        Sub Getname()

        Sub SetSal()

        Sub GetSal()

    End Interface

    Interface IInf

        Inherits IPerson, IExecutive

        Sub GetIinf()

    End Interface

    Class Emp

        Implements IInf

        Private name, sal, age As String

        Public Sub Getname() Implements IPerson.GetName, IExecutive.Getname

            WriteLine(“Name:” & name)

        End Sub

 

        Public Sub GetSal() Implements IExecutive.GetSal

            WriteLine(“Sal:” & sal)

        End Sub

 

        Public Sub SetName() Implements IExecutive.SetName, IPerson.SetName

            WriteLine(“Enter name:”)

            name = ReadLine()

        End Sub

 

        Public Sub SetSal() Implements IExecutive.SetSal

            WriteLine(“Enter Sal:”)

            sal = ReadLine()

        End Sub

 

        Public Sub GetIinf() Implements IInf.GetIinf

            WriteLine(“Inf demo”)

        End Sub

 

        Public Sub GetAge() Implements IPerson.GetAge

            WriteLine(“Age:” & age)

        End Sub

 

       

        Public Sub SetAge() Implements IPerson.SetAge

            WriteLine(“Enter age:”)

            age = ReadLine()

        End Sub

    End Class

    Sub main()

        Dim e As New Emp

        e.SetName()

        e.SetAge()

        e.SetSal()

        e.Getname()

        e.GetAge()

        e.GetSal()

    End Sub

End Module

 

Program25:

 

Option Strict On

Imports System.Console

Module Module25

    Sub main()

        Dim pet1 As New Animal

        Dim pet2 As New fish

        MyMethod(pet1)

        MyMethod(pet2)

    End Sub

    Interface IBehaviour

        Sub Breathing()

    End Interface

    Class Animal

        Implements IBehaviour

 

        Public Sub Breathing() Implements IBehaviour.Breathing

            WriteLine(“animal breaths noisly”)

        End Sub

    End Class

    Class fish

        Implements IBehaviour

 

        Public Sub Breathing() Implements IBehaviour.Breathing

            WriteLine(“Fish breaths silently”)

        End Sub

    End Class

    Sub MyMethod(ByVal i As IBehaviour)

        i.Breathing()

    End Sub

 

End Module

 

 

 

Posted in vb.net_oops_programes | Leave a Comment »