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. #20
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,521
    Rep Power
    10
    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.
    Last edited by DocAElstein; 03-03-2021 at 11:24 PM.

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
  •