Readonly properties in Visual Basic

Usually, property accessors come in pairs—that is, every property has both a Get and a Set accessor. But this isn’t always the case. You can create properties that can be read but not set (which are called read-only properties), and properties that can be set but not retrieved (called write-only). All you need to do is include either the ReadOnly or the WriteOnly keyword in the property declaration, and then leave out whichever part of the property you don’t need.

Example

Class Example1
    Private _count As Integer

    Public ReadOnly Property Number() As Integer 'readonly keyword
        Get
            Return 5000
        End Get
    End Property

End Class

Module Module1

    Sub Main()
        Dim obj As Example1 = New Example1()
        Console.WriteLine(obj.Number)

        Console.ReadLine()
    End Sub

End Module

Output

5000
Share

You may also like...