Home »
VB.Net »
VB.Net Programs
VB.Net program to overload the property
By Nidhi Last Updated : November 15, 2024
Overload the property in VB.Net
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.
VB.Net code to overload the property
'VB.net 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
Get
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
Get
For i = 0 To size Step 1
If countries(i) = val Then
Return i
End If
Next
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))
Next
If (S.Search("America") <> -1) Then
Console.WriteLine("America found in the country list")
Else
Console.WriteLine("America is not found in the country list")
End If
End Sub
End Module
Output:
Country Names:
India
Australia
America
Canada
America found in the country list
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. 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 »