I think you can only initialize ReadOnly fields when declaring them, or in the constructor:
orCode:Public ReadOnly Name As String = "ABC"
Since you are talking about a module, only the first is an option.Code:Public Readonly Name As String Public Sub New() Name = "ABC" End Sub
If you do need to set the value at a later stage, consider a ReadOnly property instead. You can still make the private backing field writable:
Now you can set the backing field to change the value of the property from within the same class (obviously not outside of the class).Code:Private _Name As String Public ReadOnly Property Name() As String Get Return _Name End Get End Property
In the same class, you can read and write:
From the outside, any other class, you can only read:Code:'Read MsgBox(Me.Name) 'Write _Name = "DEF"
Code:'Read MsgBox(classInstance.Name) 'Write classInstance.Name = "DEF" ' readonly error (Name is readonly) classInstance._Name = "DEF" ' access modifier error (_Name is private)




Reply With Quote