Table of Contents hide
Example : A Class and Object Examples in VB .NET program using Subroutine in Console Application.
Imports System

Public Class Rectangle
    Dim length As Integer
    Dim width As Integer
    Dim result As Integer

    'Private length As Integer
    'Private width As Integer
    'Private result As Integer

    Public Sub Input()
        length = 20
        width = 30
    End Sub

    Public Sub Process()
        result = length * width
    End Sub

    Public Sub Display()
        Console.WriteLine("Length: {0}", length)
        Console.WriteLine("Width: {0}", width)
        Console.WriteLine("Area: {0}", result)
    End Sub

    Shared Sub Main()
        Dim Obj1 As New Rectangle()
        Obj1.Input()
        Obj1.Process()
        Obj1.Display()
        Console.ReadLine()
    End Sub

End Class

Output:
Length: 20
Width: 30
Area: 600

-----------------  OR  -------------------

Imports System
Module Module1
    Public Class Rectangle
        Dim length As Integer
        Dim width As Integer
        Dim result As Integer

        'Private length As Integer
        'Private width As Integer
        'Private result As Integer

        Public Sub Input()
            length = 20
            width = 30
        End Sub

        Public Sub Process()
            result = length * width
        End Sub

        Public Sub Display()
            Console.WriteLine("Length: {0}", length)
            Console.WriteLine("Width: {0}", width)
            Console.WriteLine("Area: {0}", result)
        End Sub

        Shared Sub Main()
            Dim Obj1 As Rectangle = New Rectangle()
            Obj1.Input()
            Obj1.Process()
            Obj1.Display()

            Console.ReadLine()
        End Sub

    End Class
End Module

Output:
Length: 20
Width: 30
Area: 600

-----------------  OR  -------------------

Imports System
Module Module1
    Public Class Rectangle
        Dim length As Integer
        Dim width As Integer
        Dim result As Integer

        'Private length As Integer
        'Private width As Integer
        'Private result As Integer

        Public Sub Input()
            length = 20
            width = 30
        End Sub

        Public Sub Process()
            result = length * width
        End Sub

        Public Sub Display()
            Console.WriteLine("Length: {0}", length)
            Console.WriteLine("Width: {0}", width)
            Console.WriteLine("Area: {0}", result)
        End Sub
    End Class

    Sub Main()
        Dim Obj1 As Rectangle = New Rectangle()
        Obj1.Input()
        Obj1.Process()
        Obj1.Display()

        Console.ReadLine()
    End Sub
End Module

Output:
Length: 20
Width: 30
Area: 600
Example : A VB .Net program to demonstrate Private Methods using a Console application.
Class Example
    Private x, y, z As Integer

    Private Sub Input()
        x = 10
        y = 20
    End Sub

    Private Sub Process()
        z = x + y
    End Sub

    Private Sub Display()
        Console.WriteLine("The addition result is = {0}", z)
    End Sub

    Sub All()
        Input()
        Process()
        Display()
    End Sub

End Class

Module Module1
    Sub Main()
        Dim Obj As New Example()
        'Obj.Input()      'Not accessible due to private
        'Obj.Process()    'Not accessible due to private
        'Obj.Display()    'Not accessible due to private

        Obj.All()
    End Sub
End Module

Output:

The addition result is = 30
Example : A VB .Net program to demonstrate Shared(Global) Variables using a Console application.
Module Module1
    Class Example
        Public Shared num As Integer = 100

        Sub Display()
            Console.WriteLine("The Value is = {0}", num)
        End Sub

        Sub Print()
            Console.WriteLine("The Value is = {0}", num)
        End Sub

    End Class
    Sub Main()
        Dim obj As New Example()
        obj.Display()
        obj.Print()
        Console.WriteLine("The Value is = {0}", Example.num)
    End Sub
End Module

Output:
The Value is = 100
The Value is = 100
The Value is = 100

NB: Shared Variables do not need object rather called directly by class name.
Example : A VB .Net program to demonstrate Shared Methods using a Console application.
Module Module1
    Class Example
        Shared Sub Display()
            Console.WriteLine("First Function Executed")
        End Sub
        Shared Sub Output()
            Console.WriteLine("Second Function Executed")
        End Sub
    End Class

    Sub Main()
        Example.Display()
        Example.Output()
    End Sub
End Module

Output:
First Function Executed
Second Function Executed

NB: Shared methods do not need object rather called by class name.
Example : A VB .Net program to demonstrate Multiple Objects using a Console application.
Class Example
    'Private num1 As Integer
    'Private num2 As Integer
    Dim num1, num2 As Integer
    Sub input(ByVal n1 As Integer, ByVal n2 As Integer)
        num1 = n1
        num2 = n2
    End Sub
    Sub output()
        Console.WriteLine("First value is = " & num1)
        Console.WriteLine("Second value is = " & num2)
    End Sub
End Class

Module Module1
    Sub Main()
        'Dim obj1 As New Example()
        'Dim obj2 As New Example()
        Dim obj1, obj2 As New Example()

        Console.WriteLine("The data of First Objects are = ")
        obj1.input(20, 30)
        obj1.output()

        Console.WriteLine(vbCrLf & "The data of Second Objects are = ")
        obj2.input(40, 50)
        obj2.output()
    End Sub
End Module

Output:
   The data of First Objects are =
   First value is = 20
   Second value is = 30

   The data of Second Objects are =
   First value is = 40
   Second value is = 50
Example : A VB .Net program to demonstrate an Array of Objects using a Console applications
Class Example
    'Private num1 As Integer
    'Private num2 As Integer
    Dim num1, num2 As Integer
    Sub input(ByVal n1 As Integer, ByVal n2 As Integer)
        num1 = n1
        num2 = n2
    End Sub
    Sub output()
        Console.WriteLine("First value is = " & num1)
        Console.WriteLine("Second value is = " & num2)
    End Sub
End Class

Module Module1
    Sub Main()
        Dim obj(3) As Example   ' Array of Objects

        obj(0) = New Example()
        obj(1) = New Example()
        obj(2) = New Example()

        Console.WriteLine("The data of First Objects(0) are = ")
        obj(0).input(120, 130)
        obj(0).output()

        Console.WriteLine(vbCrLf & "The data of Second Objects(1) are = ")
        obj(1).input(200, 300)
        obj(1).output()

        Console.WriteLine(vbCrLf & "The data of Third Objects(2) are = ")
        obj(2).input(50, 30)
        obj(2).output()

    End Sub
End Module

Output:

The data of First Objects(0) are =
First value is = 120
Second value is = 130

The data of Second Objects(1) are =
First value is = 200
Second value is = 300

The data of Third Objects(2) are =
First value is = 50
Second value is = 30
Example : A VB .net program to Count the Total No. of Objects present/created/used in a Program in a Console applications
Module Module1
    Class Example
        Shared count As Integer = 0

        Sub New()
            count = count + 1
        End Sub

        Sub Output()
            Console.WriteLine("Total No. of Objects = {0}", count)
        End Sub

    End Class
    Sub Main()
        Dim obj1 As New Example()
        Dim obj2 As New Example()
        Dim obj3 As New Example()

        obj3.Output()
    End Sub
End Module

Outputs:
Total No. of Objects = 3
Example: A VB .NET program in Function to show the concept of class & object in Console Application.
Imports System

Public Class Rectangle
    Dim length As Integer
    Dim width As Integer
    Dim result As Integer

    Function Input() As Integer
        length = 20
        width = 30
        Return 0
    End Function

    Function Process() As Integer
        result = length * width
        Return 0
    End Function

    Function Display() As Integer
        Console.WriteLine("Length is : {0}", length)
        Console.WriteLine("Width is : {0}", width)
        Console.WriteLine("Area of rectangle is: {0}", result)
        Return 0
    End Function

    Shared Sub Main()
        Dim Obj1 As New Rectangle()
        Obj1.Input()
        Obj1.Process()
        Obj1.Display()
        Console.ReadLine()
    End Sub

End Class

Output:
Length: 20
Width: 30
Area: 600
Example: A VB .NET program with Parameterized Function to show the concept of class & object in Console Application.
Imports System

Public Class Rectangle
    Dim length As Integer
    Dim width As Integer
    Dim result As Integer

    Function Input(ByVal len As Integer, ByVal wd As Integer) As Integer
        length = len
        width = wd
        Return 0
    End Function

    Function Process() As Integer
        result = length * width
        Return 0
    End Function

    Function Display() As Integer
        Console.WriteLine("Length is : {0}", length)
        Console.WriteLine("Width is : {0}", width)
        Console.WriteLine("Area of rectangle is: {0}", result)
        Return 0
    End Function

    Shared Sub Main()
        Dim Obj1 As New Rectangle()
        Obj1.Input(15, 30)
        Obj1.Process()
        Obj1.Display()
        Console.ReadLine()
    End Sub

End Class

Output:
Length is : 15
Width is : 30
Area of rectangle is: 450
Example : A VB .NET program with Parameterized and return type Functions to show the concept of class & object in a Console Application.
Imports System

Public Class Rectangle
    Dim length As Integer
    Dim width As Integer
    Dim result As Integer

    Function Input(ByVal len As Integer, ByVal wd As Integer) As Integer
        length = len
        width = wd
        Return 0
    End Function

    Function Process() As Integer
        result = length * width
        Return result
    End Function

    Shared Sub Main()
        Dim store As Integer
        Dim Obj1 As New Rectangle()
        Obj1.Input(15, 30)

        store = Obj1.Process()
        Console.WriteLine("The output is = " & store)

        Console.ReadLine()
    End Sub

End Class

Output:
The output is = 450
Example: A VB .NET program with Parameterized & Return type Nested Function to show the concept of class & object in Console Application.
Imports System

Public Class Rectangle
    Dim length As Integer
    Dim width As Integer
    Dim result As Integer

    Function Input(ByVal len As Integer, ByVal wd As Integer) As Integer
        length = len
        width = wd
        Return 0
    End Function

    Function Process() As Integer
        result = length * width
        Return result
    End Function

    Function Output() As Integer
        Console.WriteLine("The result is = " & Process())
        Return 0
    End Function

    Shared Sub Main()
        Dim Obj1 As New Rectangle()
        Obj1.Input(30, 30)
        Obj1.Process()
        Obj1.Output()

        Console.ReadLine()
    End Sub
End Class

Output:
The result is = 900
Example: A VB .NET program in Subroutine to use Looping concept using class & object in Console Application.
Imports System

Public Class Example
    Dim x As Integer
    Dim y As Integer
    Dim result As Integer
    Dim i As Integer

    'Private length As Integer
    'Private width As Integer
    'Private result As Integer

    Public Sub Input()
        x = 20
        y = 30
    End Sub

    Public Sub Process()
        For Me.i = x To y
            Console.Write(i & " ")
        Next
    End Sub

    Shared Sub Main()
        Dim Obj1 As New Example()
        Obj1.Input()
        Obj1.Process()
        Console.ReadLine()
    End Sub

End Class

Output:
20 21 22 23 24 25 26 27 28 29 30
Example: A VB .NET program to demonstrate Global Variables in a Console Application.
Module Module1
    Dim num1 As Integer = 15    'Global variable
    Dim num2 As Integer = 25    'Global variable
    Sub Main()
        Dim output As Integer = 0    'Local variable
        output = num1 + num2
        Console.WriteLine("Addition result = " & output)
    End Sub
End Module

Output: 
   Addition result = 40
Example : A VB .NET program to demonstrate the Me (like This ) Keyword using a Console Application.
Module Module1
    Class Example
        Dim num1 As Integer
        Dim num2 As Integer

        Sub New(ByVal num1 As Integer, ByVal num2 As Integer)
            Me.num1 = num1
            Me.num2 = num2
        End Sub

        Sub Display()
            Console.WriteLine("First Accepted Value is = {0}", Me.num1)
            Console.WriteLine("Second Accepted Value is = {0}", Me.num2)

            Console.WriteLine("First Parameterized Value is = {0}", num1)
            Console.WriteLine("Second Parameterized Value is = {0}", num2)
        End Sub
    End Class

    Sub Main()
        Dim obj As New Example(40, 50)
        obj.Display()
    End Sub
End Module

Output:
First Accepted Value is = 40
Second Accepted Value is = 50

First Parameterized Value is = 40
Second Parameterized Value is = 50

NB: In VB .Net, the Me keyword/object is used within the class to access the data members or to a point-current object.
Example : A VB .NET program to demonstrate the MyClass Keyword using a Console Application.
Module Module1
    Class Example
        Sub Display1()
            Console.WriteLine("First Display Function is called")
        End Sub

        Sub Display2()
            MyClass.Display1()
            Console.WriteLine("Second Display Function is called")
        End Sub
    End Class

    Sub Main()
        Dim obj As New Example()
        obj.Display2()
    End Sub
End Module

Output:
First Display Function is called
Second Display Function is called

NB: MyClass keyword is used to refer to the existing class name.
Example: A VB .NET program in Subroutine to use Function/Method Overloading using class & object in Console Application.
Module Module1

    Public Class Calculate
        Public Sub Addition(ByVal x As Integer, ByVal y As Integer)
            Console.WriteLine("x + y = {0}", x + y)
        End Sub

        Public Sub Addition(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer)
            Console.WriteLine("x + y + z = {0}", x + y + z)
        End Sub
    End Class

    Sub Main(ByVal args As String())
        Dim Obj As Calculate = New Calculate()
        Obj.Addition(10, 20)
        Obj.Addition(10, 20, 30)
        Console.WriteLine("Press Enter Key to Exit..")
        Console.ReadLine()
    End Sub

End Module

Output:
x + y = 30
x + y + z = 60
Press Enter Key to Exit..

---------------------  OR  ---------------------

Module Module1
    Public Class Calculate
        Public Sub Addition(ByVal x As Integer, ByVal y As Integer)
            Console.WriteLine("x + y = {0}", x + y)
        End Sub

        Public Sub Addition(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer)
            Console.WriteLine("x + y + z = {0}", x + y + z)
        End Sub

        Shared Sub Main(ByVal args As String())
            Dim Obj As Calculate = New Calculate()
            Obj.Addition(10, 20)
            Obj.Addition(10, 20, 30)
            Console.WriteLine("Press Enter Key to Exit..")
            Console.ReadLine()
        End Sub
    End Class
End Module

Output:
x + y = 30
x + y + z = 60
Press Enter Key to Exit..
Example : Write a VB.Net Program to demonstrate the Friend Function/Method using Console Applications.
Class Example
    ' The Fun() is public if we are in the same assembly.
    Friend Sub Display()
        Console.WriteLine("Friend function Executed")
    End Sub
End Class

Module Module1
    Sub Main()
        Dim obj As New Example()
        obj.Display()
    End Sub
End Module

Output:
Friend Function Executed

NB:The Friend keyword is used in the declaration of friend method and class that specifies the elements are accessible from within the same assembly. It can be accessed anywhere in the same assembly, but not from other assemblies. This is the default modifier for GUI components in Visual Studio
Example: A VB .NET program to demonstrate Non-Member Functions/Methods Overloading in Console Application.
Module Module1
    Sub Addition(ByVal num1 As Double, ByVal num2 As Integer)
        Dim sum1 As Double = 0
        sum1 = num1 + num2
        Console.WriteLine("Sum is: {0}", sum1)
    End Sub

    Public Sub Addition(ByVal num1 As Integer, ByVal num2 As Double)
        Dim sum2 As Double = 0
        sum2 = num1 + num2
        Console.WriteLine("Sum is: {0}", sum2)
    End Sub

    Sub Main()
        Addition(45.36, 82)
        Addition(56, 8.23)
    End Sub

End Module

Output:
Sum is: 127.36
Sum is: 64.23
Example :Class and Object Examples in VB .NET program with Empty Class using Console Application.
Class Example

End Class

Module Module1
    Sub Main()
        Dim obj As New Example

        Console.WriteLine("Type name is = {0}", obj.GetType())
    End Sub
End Module

Output: Type name is = ConsoleApp3.Example   [Here ConsoleApp3 is saved project name and Example is the class name]
Example :Class and Object Examples in VB .NET program to demonstrate the Boxing concept.
Module Module1
    Sub Main()
        Dim val As Integer = 100
        Dim obj As Object
        obj = val
        Console.WriteLine("Value of Integer in Object form is = " & obj)
    End Sub
End Module

Output:
Value of Integer in Object form is = 100
Example : A VB .NET program to demonstrate the Un-boxing concept.
Module Module1
    Sub Main()
        Dim val As Integer
        Dim obj As Object = 100

        val = CInt(obj)
        Console.WriteLine("Value of Object in Integer form is = " & val)
    End Sub
End Module

Output:
Value of Object in Integer form is = 100 
Example : Write a Class and Object Examples in VB.Net Program to Demonstrate Read Only Properties using Console Applications.
Class Student
    Dim Rollno As Integer
    Dim StuName As String

    Sub New(ByVal rn As Integer, ByVal sn As String)
        Rollno = rn
        StuName = sn
    End Sub

    Public ReadOnly Property StudentID As Integer
        Get
            Return Rollno
        End Get
    End Property

    Public ReadOnly Property StudentName As String
        Get
            Return StuName
        End Get
    End Property
End Class

Module Module1
    Sub Main()
        Dim Stu As New Student(310, "Azad")
        Console.WriteLine("Student Rollno is = {0}", Stu.StudentID)
        Console.WriteLine("Student Name is = {0}", Stu.StudentName)
    End Sub
End Module

Output:
Student Rollno is = 310
Student Name is = Azad
Example : Write a Class and Object Examples in VB.Net Program to Demonstrate Write Only Properties using Console Applications.
Class Student
    Dim Rollno As Integer
    Dim StuName As String

    Public WriteOnly Property StudentID As Integer
        Set(rnvalue As Integer)
            Rollno = rnvalue
        End Set
    End Property

    Public WriteOnly Property StudentName As String
        Set(snvalue As String)
            StuName = snvalue
        End Set
    End Property

    Sub Display()
        Console.WriteLine("Student Rollno is = {0}", Rollno)
        Console.WriteLine("Student Name is = {0}", StuName)
    End Sub
End Class

Module Module1
    Sub Main()
        Dim Stu As New Student()
        Stu.StudentID = 240
        Stu.StudentName = "Rani"
        Stu.Display()
    End Sub
End Module

Output:
Student Rollno is = 240
Student Name is = Rani
Example : Write a Class and Object Examples in the VB.Net Program to Demonstrate Set and Get Properties using Console Applications.
Class Student
    Dim Sname As String
    Dim Rollno As Integer
    Public Property StudentRollno As Integer
        Get
            Return Rollno
        End Get

        Set(Value As Integer)
            Rollno = Value
        End Set

    End Property
    Public Property StudentName As String
        Get
            Return Sname
        End Get

        Set(Value As String)
            Sname = Value
        End Set

    End Property
End Class

Module Module1
    Sub Main()
        Dim Stu As New Student()

        Stu.StudentRollno = 301
        Stu.StudentName = "Raman"

        Console.WriteLine("Student Rollno  : {0}", Stu.StudentRollno)
        Console.WriteLine("Student Sname: {0}", Stu.StudentName)
    End Sub
End Module

Output:
Student Rollno  : 301
Student Sname: Raman

Loading


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.