Special procedures in Class text module
Public Variable Properties 2
Extended use of the special Class module text Procedures.
The previous example was a simplified use to show the direct comparison with how they might typically be used to achieve the same as the simple Public variable as property. But we have much more flexibility due to these extra special procedures. Here are just a few notes and examples of some of the extra possibilities
Extra coding text within the procedures
Most normal VBA coding text is accepted inside the procedures. A simple example could be coding inside the Public Property Let to check the data given, and take action, such as a message box with a warning, followed by Exiting if the supplied data is unacceptable ..._
___If Len(Clr) > 20 Then MsgBox prompt:="That is too long for a color word": Exit Property
_... … etc.. etc… etc… The possibilities are almost endless.
Extra arguments
In addition, the syntax allows us to bring in any other variables, which are added before the main variable in the Public Property Let signature line
For example,
Public Property Let CrColor(Optional CrsNme As String, Clr As String)
for syntax reasons we must duplicate the extra information exactly in the paired Public Property Get
Public Property Get CrColor(Optional CrsNme As String) As String
( I am not restricted to Optional argument. The coding works ijn the conventional way regarding the argument definitions: If I did not choose Optional , then I would have to give them in any usage or I would get an error )
These extra arguments appear to be held internally somewhere as a form of pseudo global variable.
Demo example
To demonstrate this: Lets say we want to optionally provide a Car name, when giving the car color.
This would be the slight modification necessary in the class module test:
In class module Car
Code:
Private PrvteCrColor As String
Public Property Let CrColor(Optional CrsNme As String, Clr As String)
Let PrvteCrColor = CrsNme & " is " & Clr
End Property
Public Property Get CrColor(Optional CrsNme As String) As String ' this property returns the value, As String
Let CrColor = PrvteCrColor
End Property
We can now add some code lines to our macro in a normal code module to demonstrate the use of the extra lines thus:
In any normal code module
Code:
Sub My_CarColor()
Dim objCr As Car: Set objCr = New Car ' This is the normal codelines used to typically make an object variable of a particular Class
Let objCr.CrColor = "Yellow" ' "write" to the CrColor variable in objCr
MsgBox Prompt:="I have colored my Car " & objCr.CrColor ' "read" (Get) from the CrColor variable in objCr
Let objCr.CrColor("Mustang") = "Yellow"
MsgBox Prompt:=objCr.CrColor '
End Sub
The initial code lines work similarly to previously. The extra two lines at the end make use of the new argument possibility.
The first message box gives _ I have colored my Car _ is Yellow
The second message box gives _ Mustang is Yellow
Notes:
_1 ) Argument types
We have chosen arbitrarily in the last example, the extra argument to be Optional in this example, but the general rules associated with procedures and Functions apply , such that , for example , if we had not included the Optional statement in the signature line, then we would have had to give a value for this in both of the Let code lines in the above normal code module macro. Because we used Optional , we were able to use the previous code lines in addition to the new one which makes uses of any supplied argument value
_2) Review of paired nature and syntax structure
_2)(i) We do not have to have a pair, since the absence of one of the other gives us the possibility to restrict the property on the instantiated object to be just either read or write. ( *I personally would usually keep the things in pairs and then ‘comment out as/ if necessary the procedure not required. I would do this as the syntax is a bit tricky/ complicated and easy to mix up the slight differences in the matched pairs )
_2)(ii) *Assuming we use a pair , we can summarise the syntax as follows:
There must be at least one argument, say, Nme , ( the last one ), in the Let …_
_ Public Property Let Xyz ( ___ , ___ , Nme As Vrtyp ) ‘ takes in Nme via code line in normal macro like Let Onj.Xyz = “Myname”
_ … The corresponding Get , below , returns a value in a similar way to how a normal VBA Function does. In the simplest case it returns the value brought in by the Let, ( Nme in this example ). Or it may have some relation to the value taken in by the Let. Or it can be something totally different. Or it can do something else and return no value, just as we sometimes do with a normal VBA Function. In this latter case there is no advantage over a simple procedure text in the class module, which also acts as a method of an object instantiated from the class.
_ Public Property Get Xyz ( ___ , ___ , _ ) As Vrtyp ‘ This works similar to a standard VBA Function.
In simple general terms, to relate to standard VBA things, we might refer to the .Xyz property, as we would, for example to the standard VBA .Value property
_.____
!!! Public Property Set
In line with normal VBA declaration conventions and syntaxes, the word Set is used in place of Let if we are using an object variable type, ( in the above shown here : Vrtyp ) which is a an object
_ Public Property Set Xyz ( ___ , ___ , Obj As VrObject ) ‘
There is nothing new , special, or seemingly particular useful with the Public Property Set.
In the next post, I will do a simple example and possibly add later any example I ever find useful.
Bookmarks