The ReadOnly and WriteOnly Properties
There may be times when you would like to have a property that can only be read or that can only be written. For example, you might have a property named mSerialNumber that holds the serial number of an oven. (This is not as farfetched as it might seem because many modern ovens are controlled by microprocessors.) There is no reason you would want the user to be able to write a new serial number. You could make the property read-only by using the following code fragment:
Public ReadOnly Property GetSerialNumber() Get End Get End Property
When you press the Enter key after the first line of code, Visual Basic .NET automatically supplies the skeletal code for the Get statement block. The ReadOnly keyword causes Visual Basic .NET to omit the Set statement block because it is not needed with a ReadOnly property.
Similarly, if you had a situation in which you wanted to write to a member variable but not make its property part of the class interface, you would use the WriteOnly keyword. For example, an oven might track the total elapsed time that it has been in use but not make that information available through the interface. (Such information might be useful in product-life studies, but you wouldn't want the user to have access to the data.) In that case, you might use something like this:
Public WriteOnly Property SetHours() Set(ByVal Value) End Set End Property
Again, Visual Basic .NET automatically supplies the code skeleton after you enter the first line. The Get statement block is omitted because the property is write-only. All you need to do is flesh out the details for the Set statement block.
Although write-only procedures are less common than read-only procedures, they provide a means for further protecting class data items from outside contamination.