Home -> Programming -> Objects -> Property Declaration

Property Declaration

While a programmer can use the Getters and Setters described in the previous section, Visual Basic has another, and better way, to accomplish the same thing. It is called a Property Declaration. A property declaration, is equivalent to both the Getter and Setter in the previous section. For the variable called pName, it would look like this:

Public Class Person

    Private pName as String

    Private Property Name() As String
    'Property declaration to get and set the contents of the name variable
        Get
            Return pName
        End Get
        Set(ByVal value As String)
            pName = value
        End Set
    End Property
End Class

There are several direct benefits of using property declarations. Having both Getter and Setter located in one subroutine simplifies the coding. Furthermore we only need one name for our procedure instead of two. Another benefit of using the property declaration is that much of this code is typed in for you. For the above declaration you would type the following and press enter:

    Private Property Name() As String

Then the following will be automatically created by the editor:

        Get

        End Get
        Set(ByVal value As String)

        End Set
    End Property

This means that the only thing we have to type for the Get part of the property is the Return statement. For the Set part of the property we assign value to the the variable pName = value. There are two precautions which a programmer must observe in this process. First, the data type of value must be the same as the data type of the property declaration and the corresponding variable! Second, the names of the variable and the property declaration must be different! Most programmers keep them similar, but they cannot be the same.

Sometimes we declare a property which has a Get but no Set. If this case the value of the property is usually set by a calculation. This calculation must take place before any data can be supplied by the object. More on this later. This type of a property declaration is called a Read Only declaration. For example if we have birth date and age variables, we would make a normal property declaration for the birth date variable but age is determined by birth date, so there is no need to have the user input the age, it is calculated based on the birth date. Notice that the calculation can not be made until we have set the birth date, so we calculate pAge immediately after the birth date is set as part of the set property of the birth date. (The other place for calculation to take place is in the ReadOnly property just before the data is returned to the calling object.) This code would look like this:

    Private pBirthDate as Date
    Private pAge as Integer

    Private Property BirthDate() As Date
    'Property declaration to get and set the birth date
        Get
            Return pBirthDate
        End Get
        Set(ByVal value As Date)
            pBirthDate = value
            If (Now.Day < pBirthDate.Day) And (Now.Month < pBirthDate.Month) Then
                pAge = Now.Year - pBirthDate.Year - 1     'The birthday has not happened yet this year
            Else
                pAge = Now.Year - pBirthDate.Year         'The birthday has already occurred this year
            End If
        End Set
    End Property

    Private ReadOnly Property Age() As String
    'Property declaration for Age
        Get
            Return pAge
        End Get
    End Property

One last important item needs to be mentioned. If we wish to add an object to a Listbox, then we want the object to be uniquely identified in the listbox. In our example above, it would be useful to display the name. While we could assign the Name property to the listbox, it is more common to do this in another way. To do this we add a method to the Person object which causes the display we desire, the name, to appear in the listbox. This requires us to override a function called .ToString. This allows us to do the assignment with less typing. For our Person object the code would look like this:

    Public Overrides Function ToString() As String
        Return Name
    End Function

Notice that the Return statement sends the contents of the Name property when the .ToString function is invoked. You will see an example of how to use this in the next section.