Class Modules – Unbound Forms ii – Class Module Properties

In this post we will be adding properties and get/let methods to the class module.

The Code

Please create a class called clsFilms and add in this code:

Private m_ID As Long
Private m_FilmName As String
Private m_YearOfRelease As String
Private m_RottenTomato As String
Private m_Director As Long

''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''Properties''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''

''''''''''''''''ID''''''''''''''''''''''''''

Public Property Get ID() As Long
ID = m_ID
End Property

''''''''''''''''FilmName''''''''''''''''''''
Public Property Get FilmName() As String
FilmName = m_FilmName
End Property

Public Property Let FilmName(value As String)
If Not IsNull(value) Then
    m_FilmName = value
End If
End Property

''''''''''''''''YearOfRelease'''''''''''''''
Public Property Get YearOfRelease() As String
YearOfRelease = m_YearOfRelease
End Property

Public Property Let YearOfRelease(value As String)
If Not IsNull(value) Then
    m_YearOfRelease = value
End If
End Property

''''''''''''''''RottenTomatoRating''''''''''
Public Property Get RottenTomato() As String
RottenTomato = m_RottenTomato
End Property

Public Property Let RottenTomato(value As String)
If Not IsNull(value) Then
    m_RottenTomato = value
End If
End Property

''''''''''''''''Director''''''''''''''''''''
Public Property Get Director() As Long
Director = m_Director
End Property

Public Property Let Director(value As Long)
If Not IsNull(value) Then
    m_Director = value
End If
End Property

The Explanation

Please remember that class modules don’t work in quite the same way as standard modules. They require declaration and instantiation and then provide access to properties and methods.

The Properties

Private m_ID As Long
Private m_FilmName As String
Private m_YearOfRelease As String
Private m_RottenTomato As String
Private m_Director As Long

The properties above are very similar in declaration to variables in standard modules. Well, exactly the same, in fact. The only difference is that in class modules we refer to them as properties and they should always be prefixed with m_ as a coding standard.

These properties are private which means that they can only be seen from within the class module. This is the correct way to create properties in a class module as you should not allow direct access to your properties or all hell can break loose.

Access To Properties

''''''''''''''''ID''''''''''''''''''''''''''

Public Property Get ID() As Long
ID = m_ID
End Property

''''''''''''''''FilmName''''''''''''''''''''
Public Property Get FilmName() As String
FilmName = m_FilmName
End Property

Public Property Let FilmName(value As String)
If Not IsNull(value) Then
    m_FilmName = value
End If
End Property

''''''''''''''''YearOfRelease'''''''''''''''
Public Property Get YearOfRelease() As String
YearOfRelease = m_YearOfRelease
End Property

Public Property Let YearOfRelease(value As String)
If Not IsNull(value) Then
    m_YearOfRelease = value
End If
End Property

''''''''''''''''RottenTomatoRating''''''''''
Public Property Get RottenTomato() As String
RottenTomato = m_RottenTomato
End Property

Public Property Let RottenTomato(value As String)
If Not IsNull(value) Then
    m_RottenTomato = value
End If
End Property

''''''''''''''''Director''''''''''''''''''''
Public Property Get Director() As Long
Director = m_Director
End Property

Public Property Let Director(value As Long)
If Not IsNull(value) Then
    m_Director = value
End If
End Property

As we can’t directly access out properties we have to provide a method to read and sometimes write them.

Reading A Property

Public Property Get FilmName() As String
FilmName = m_FilmName
End Property

In order to read a property, we need a property get method.

Public Property Get FilmName() As String 

The name of the get property is FilmName and it will return a value of data type String. Think of these as functions that return a value.

FilmName = m_FilmName 

We set the return value to be equal to the value of m_FilmName.

Writing A Property

Public Property Let FilmName(value As String)
If Not IsNull(value) Then
    m_FilmName = value
End If
End Property

In order to write a property we need a property let method.

Public Property Let FilmName(value As String)

In our case the name of the method is FilmName and it accepts one argument value As String. This method is very similar to a sub-procedure in that it performs an action but doesn’t return a value.

If Not IsNull(value) Then
    m_FilmName = value
  End If

Here we are seeing the real power of having private variables. We are using this code to update the value of m_FilmName ONLY IF  the passed in argument value As String is not null. In this way we can protect our variables from bad input.

Making A Property Read-Only

In order to make a property read-only, we simply provide a Get property but not a Let property. In our code example above, the m_ID Property is read-only.

Here is the database with the added code:

Leave a Reply

Your email address will not be published. Required fields are marked *

Visit Us On TwitterVisit Us On FacebookVisit Us On Youtube