Special procedures in Class text module
Public Variable Properties 2
Public Variable Properties 2
Simple use – used as in Public Variable Properties 1
The best way to explain these strange pair of special procedure text is to compare their use to do the same as in our simple example of the car color property: Previously our variable was written at the top of the Class car module as variable, Public CrColor As String , and we noted that it was read and write , as demonstrated by our simple macro
To do the same in a more complicated way, using the special procedure text, we start by removing the Public CrColor As String from the top of the class module, since the property, CrColor will now be defined by the special procedures. – Simply said, it becomes the name of the procedure pair , ( Both have this same name ). We do however, still need to store our string color in a variable. We would typically do this by a Private variable in the instantiated object. Correspondingly, the text in the Class module must be there to make that code line appear in any instantiated instance. So something like this in the class module would do, ( It will become clearer later why exactly we need to do this )Code:Sub MyCarColor() 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" from the CrColor variable in objCr End Sub
Private PrvteCrColor As String
We know that this variable , as it appears in any instantiated object, is Private , and therefore will not Let itself be assigned via a code line in a normal module of like Let objCr. PrvteCrColor = "Yellow" . we need a way to fill the variable, from a normal code line. That is what the first of the special procedures does, or rather this is one of its most usual uses.
Here is that special procedure, along with the private variable discussed, in the class module, Car
Our previous simple macro in a normal code module will now work, at least initially: Once we instantiate our object, objCr to an instance of Car , the code line, Let objCr.CrColor = "Yellow" will recognise our specially defined property , CrColorCode:' Public CrColor As String Private PrvteCrColor As String Public Property Let CrColor(Clr As String) Let PrvteCrColor = Clr End Property
The way the Public Property Let CrColor(Clr As String) then works is possibly is expected: It takes into the variable Clr, the text “Yellow”.
We then assign that string to the private variable: In the code text inside the procedure, we are allowed to use most VBA coding. We have chosen to assign the private variable, PrvteCrColor , the value taken in via Clr
So to review where we are… put this text in the Class module
Now try running again the below simple macro in any normal code module ( it should error ) :Code:Option Explicit ' Public CrColor As String Private PrvteCrColor As String Public Property Let CrColor(Clr As String) Let PrvteCrColor = Clr End Property
No Read No Getting of the Car color.JPG : https://imgur.com/BXqE6LyCode:Sub MyCarColor() 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" from the CrColor variable in objCr End Sub
No Read No Getting of the Car color.JPG
The error , Prohibited use of a property , comes at the attempt to Get at the property.
As you may have guessed, this is where the second main special class text procedure comes in. This takes the syntax and form similar to a normal VBA Function ###. A code line in a normal code module like our objCr.CrColor calls it into working, and if we want it to return a value, then as in the case of a Function we must ,( usually towards the end of the procedure ) , assign the function to the value to be returned, in our case the stored color string in the private variable, like
Let CrColor = PrvteCrColor
So to summarise, here would be the full class module text, and normal coding to do the read and write of our property using the typical special class procedure pair.
In Class text module
In normal code moduleCode:' Public CrColor As String Private PrvteCrColor As String Public Property Let CrColor(Clr As String) Let PrvteCrColor = Clr End Property Public Property Get CrColor() As String ' this property returns the value, As String Let CrColor = PrvteCrColor End Property
###( One of the many varied, ( and unfortunately often inconstant ) definitions of a method is that it is a function of an object. We can see how this definition fits in here.)Code:Sub MyCarColor() 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 End Sub
() …. bit confusing Syntax
The Public Property Get xxxx() As Sxxyyz has a familiar syntax to the standard Function , having either nothing or arguments to be passed inside the ( )
The Public Property Let for some strange reason has the _ As Sxxyyz brought into the ()
One possible explanation for this is that we must somehow be able to refer to the Property value taken in.
The equivalent for the case of the Get is that the thing itself is the return value. In our example we do any referring in a code line like this:
Public Property Get CrColor() As String
Let CrColor = PrvteCrColor
End Property
which is the typical function value giving code line , typically towards the end of a function.
For the case of the Public Property Let we could possibly think it would look like
Public Property Let CrColor() Clr As String
Let PrvteCrColor = Clr
End Property
Although that might seem reasonable, it possibly does not “fit” very well into typical VBA code line structures. A more conventional looking way such as the following . possibly fits better:
Public Property Let (CrColorAs String)
Let PrvteCrColor = Clr
End Property
The end result of this, which we will see a bit more clearly in the next post, is that the number of arguments in the Public Property Let must be at least 1 and can be N+1 where N is the number of augments we have in the Public Property Get
N is the number of arguments we choose to have for the Property.
( Just to remind us: in the simple Public Variable Properties 1 way we don’t have any arguments in this sense. These arguments, N , are one of the extra possibilities that we have due to the more complicated Public Variable Properties 2 way. This is discussed in more detail in the next post
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgzMCQUIQgrbec400jl4AaABAg
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgwhVTFaD469mW9wO194AaABAg.9gJzxwFcnPU9gORqKw5t W_
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugyb8nmKKoXvcdM58gV4AaABAg
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgwvvXcl1oa79xS7BAV4AaABAg
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgxvIFArksPprylHXYZ4AaABAg
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA




Reply With Quote
Bookmarks