VB.Net program to demonstrate the default property

Here, we are going to demonstrate the default property in VB.Net.
Submitted by Nidhi, on January 02, 2021

Here, we will create a class and implement default property to set and get values of data members using the object of the class.

Program/Source Code:

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

'VB.net program to demonstrate default property.

Class Sample
    Dim countries(5) As String
    Default Public Property country(ByVal index As Integer) As String
        Get
            Return countries(index)
        End Get
        Set(ByVal value As String)
            countries(index) = value
        End Set
    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))
        Next
    End Sub
End Module

Output:

Country Names:
        India
        Australia
        America
        Canada
Press any key to continue . . .

Explanation:

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.

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.

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

© https://www.includehelp.com some rights reserved.