Properties are values stored by a class. Simple properties
, often called fields, may just be public variables within the class, as shown by the
Value properties of the
Message class earlier. More complex
properties are created using
Why would a property need to be complex? Several possible reasons:
Most often, properties are complex if they represent a value that is calculated in some way, such as the count of a list of items.
In other cases, a property may represent information that can be read, but not changed. These are called read-only properties.
Less often, a property may represent information that can be set only once, but never changed. These are called write-once properties.
Finally, a property may represent a value that can be set but never read. You almost never need to do that, but if you do, you’d call it a write-only property.
Let’s continue on with the
Message class example a bit to create two new properties that extend its email capabilities. The
Recipients property that follows is another simple property that accepts a list of email addresses to send the message to:
' Message class Public Recipients As String
To use this property from the
Send method, we make these changes shown in bold:
Public Sub Send(
OptionalToAddress As String) Dim msgToSend As String, result As Double If (ToAddress = "") Then ToAddress = Recipients msgToSend = "mailto:" & ToAddress msgToSend = msgToSend & "?SUBJECT=" & Title msgToSend = msgToSend ...