Example : A Constructor Examples in VB .net to show the parameterized constructor concept.
Imports System
Module Module1
    Public Class Person
        Private name As String
        Private salary As Integer

        Public Sub New()
            name = "Robert"
            salary = 75000

            Console.WriteLine(name)
            Console.WriteLine(salary)
        End Sub

        Public Sub New(ByVal name1 As String, ByVal salary1 As Integer)
            name = name1
            salary = salary1

            Console.WriteLine()
            Console.WriteLine(name)
            Console.WriteLine(salary)
        End Sub

    End Class

    Sub Main()
        Dim obj1 As New Person()
        Dim obj2 As New Person("Sonia", 60000)

        Console.ReadKey()
    End Sub
End Module

Output:
Robert
75000

Sonia
60000

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

Imports System
Module Module1
    Public Class Person
        Private name As String
        Private age As Integer

        Public Sub New(ByVal name1 As String, ByVal age1 As Integer)
            name = name1
            age = age1
        End Sub

        Public Sub display()
            Console.WriteLine(name)
            Console.WriteLine(age)
        End Sub
    End Class

    Sub Main()
        Dim obj As New Person("John", 60)
        obj.display()
        Console.ReadKey()
    End Sub
End Module
---------------------  OR  --------------------

Imports System
Module Module1
    Sub Main()
        Dim con As New Message("Hello world")  'con object creation'

        Console.WriteLine(con.display())        'display method calling'
        Console.ReadKey()
    End Sub
End Module

Public Class Message
    Public Str As String

    Public Sub New(ByVal X As String)         
        Str = X
    End Sub

    Public Function display() As String
        Return Str
    End Function

End Class

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

Imports System
Public Class Message
    Public Str As String

    Public Sub New(ByVal X As String)         
        Str = X
    End Sub

    Public Function display() As String
        Return Str
    End Function

End Class

Module Module1
    Sub Main()
        Dim con As New Message("Hello world")  'con object creation'

        Console.WriteLine(con.display())        'display method calling'
        Console.ReadKey()
    End Sub
End Module

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

Imports System
Public Class Message
    Public x, y As Integer
    Public Sub New()
        Console.WriteLine("Function Return type Constructor")
    End Sub
    Public Sub New(ByVal m As Integer, ByVal n As Integer)
        x = m
        y = n
    End Sub

    Public Function display() As String
        Return x
    End Function

    Public Function output() As String
        Return y
    End Function
    Public Function final() As String
        Console.WriteLine(display())
        Console.WriteLine(output())
        Return 0
    End Function

End Class

Module Module1
    Sub Main()
        Dim obj1 As New Message()  'con object creation'
        Dim obj2 As New Message(20, 30)
        obj2.final()

        Console.ReadKey()
    End Sub
End Module

Output:
Function Return type Constructor
20
30

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

Imports System
Class Message
    Dim Str As String

    Sub New(ByVal X As String)         
        Str = X
    End Sub

    Function display() As String
        Return Str
    End Function

End Class

Module Module1
    Sub Main()
        Dim con As New Message("Hello world")  'con object creation'
        Console.WriteLine(con.display())        'display method calling'
        Console.ReadKey()
    End Sub
End Module
Example : A Constructor Examples in VB .net to show the constructor overloading concept.
Imports System
Module Module1
    Public Class General
        Private x, y, z As Integer

        'Dim x, y, z As Integer

        Public Sub New()
            Console.WriteLine("Constructor overloading")
        End Sub

        Public Sub New(ByVal x1 As Integer)
            x = x1

            Console.WriteLine()
            Console.WriteLine(x)
        End Sub

        Public Sub New(ByVal x2 As Integer, ByVal y1 As Integer)
            x = x2
            y = y1

            Console.WriteLine()
            Console.WriteLine(x)
            Console.WriteLine(y)

        End Sub

    End Class
    Sub main()
        Dim obj1 As New General()
        Dim obj2 As New General(100)
        Dim obj3 As New General(500, 1000)

        Console.ReadKey()
    End Sub
End Module

Output:
Constructor overloading

100

500
1000
Example : A VB .Net program to show the Shared constructor concept.
Imports System

Class A
    Shared Sub New()      
        Console.WriteLine("Class A constructor initiated")     
    End Sub
End Class

Class B
    Inherits A

    Shared Sub New()
        Console.WriteLine("Class B constructor initiated")
    End Sub

    Public Shared Sub Message()
        Console.WriteLine("Hello world")
    End Sub

End Class

Module Testcons
    Sub Main()
        Console.WriteLine("50")
        B.Message()
        Console.WriteLine("100")
        Console.ReadKey()
    End Sub
End Module

Output:
50
Class B constructor initiated
Hello world
100

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.