Results 1 to 10 of 30

Thread: Class Stuff: VBA Custom Classes & Objects, Class Modules

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #9
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    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
    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
    
    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 )
    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
    Code:
    ' Public CrColor As String
    Private PrvteCrColor As String
    Public Property Let CrColor(Clr As String)
     Let PrvteCrColor = Clr
    End Property
    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 , CrColor
    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
    Code:
    Option Explicit
    ' Public CrColor As String
    Private PrvteCrColor As String
    Public Property Let CrColor(Clr As String)
     Let PrvteCrColor = Clr
    End Property
    Now try running again the below simple macro in any normal code module ( it should error ) :
    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
    
    No Read No Getting of the Car color.JPG : https://imgur.com/BXqE6Ly
    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
    Code:
    ' 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
    In normal code module
    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
    ###( 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.)



    () …. 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
    Last edited by DocAElstein; 07-11-2023 at 12:54 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    KILL A MODERATOR!!

Similar Threads

  1. PQ - IP C class generator
    By sandy666 in forum ETL PQ Tips and Tricks
    Replies: 0
    Last Post: 10-22-2020, 05:16 AM
  2. Backup all modules, class modules and userforms to a selectable folder
    By MrBlackd in forum Excel and VBA Tips and Tricks
    Replies: 1
    Last Post: 04-06-2014, 08:33 AM
  3. Manipulate VBA Array Object Using Class Module
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 0
    Last Post: 06-06-2013, 07:53 PM
  4. Array Class Module
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 3
    Last Post: 12-20-2012, 11:22 AM
  5. Class Objects Created Using the CreateObject Method That Employs Late Binding
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 08-16-2011, 12:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •