×

VB.Net Programs

VB.Net program to demonstrate the default property

By Nidhi Last Updated : November 11, 2024

Default property in VB.Net

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 code to demonstrate the example of default property

'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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.