VB.Net program to overload the property

Here, we are going to learn how to overload the property in VB.Net?
Submitted by Nidhi, on January 03, 2021

Here, we will create a class and implement default property to set and get values of data members, and we also created one more property to searched the specified county in the country list.

Program/Source Code:

The source code to overload the property is given below. The given program is compiled and executed successfully.

' program to overload the property.

Class Sample
    Dim countries(5) As String
    Dim size As Integer
    Default Public Property country(ByVal index As Integer) As String
            Return countries(index)
        End Get
        Set(ByVal value As String)
            size = index
            countries(index) = value
        End Set
    End Property

    Public ReadOnly Property Search(ByVal val As String) As Integer
            For i = 0 To size Step 1
                If countries(i) = val Then
                    Return i
                End If
            Return -1
        End Get
    End Property
End Class
Module Module1
    Sub Main()
        Dim S As New Sample()

        S(0) = "India"
        S(1) = "Australia"
        S(2) = "America"
        S(3) = "Canada"

        Console.WriteLine("Country Names:")
        For i = 0 To 3 Step 1
            Console.WriteLine(vbTab & S(i))

        If (S.Search("America") <> -1) Then
            Console.WriteLine("America found in the country list")
            Console.WriteLine("America is not found in the country list")
        End If
    End Sub
End Module


Country Names:
America found in the country list
Press any key to continue . . .


In the above program, we created a class Sample that contains a data member countries. Here, countries is an array of strings. Here, we implemented a default property to set and get the values of the countries array. Then we also implemented one more property to search the country name from the list of countries, here if the country name is found then it will return the index of country name otherwise it will return -1.

After that, we created a module Module1 that contains the Main() method, the Main() method is the entry point for the program. And, we created an object of the Sample class and then set the values of data members using object name with specified index and then print the country names on the console screen. After that, we searched the item from the list and print the appropriate message on the console screen.

VB.Net Basic Programs »



Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.