Using Property

The Property Get procedure retrieves the value of a class property. Its declaration is much the same as a standard VBA function, but with the addition of the Get keyword. Like a function, you declare sits return data type to that of the class property it will return. Whatever receives the procedure's return value must be declared with the same data type.

For example, the following code is the Dogs Property Get procedure from our Kennel class example:

Public Property Get Dogs() As Integer

Dogs = mintDogs End Property

The name Dogs defines the name of the class's property as far as VBA code is concerned. Its return data type is declared as integer, and when VBA code calls the property like so:

intSomeVariable = myKennel.Dogs

VBA calls the procedure just like any standard function, and the code inside returns the privately declared property mintDogs. Property procedures can do anything a standard procedure can do, even accept arguments, but in practice, that is rarely done. Usually, since methods act on data in ways that often depend on other values or conditions, they tend to be used to accept arguments. Referring to an argument declared in a Property Get procedure is simple enough. For example, if we declare our procedure like so:

Public Property Get Dogs(strColor

As String) As Integer

' Code that uses the strColor


Dogs = mintDogs

End Property

We can refer to it like this:

intSomeVariable = myKennel.Dogs("black")

0 0

Post a comment