O'Reilly logo

Programming Excel with VBA and .NET by Steve Saunders, Jeff Webb

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Create Properties

Properties are values stored by a class. Simple properties , often called fields, may just be public variables within the class, as shown by the Title and Value properties of the Message class earlier. More complex properties are created using Property procedures.

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(Optional ToAddress As String)
        Dim msgToSend As String, result As Double
        If (ToAddress = "") Then ToAddress = Recipients msgToSend = "mailto:" & ToAddress msgToSend = msgToSend & "?SUBJECT=" & Title msgToSend = msgToSend ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required