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. #8
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Public Variable Properties 1
    We discussed in the case of the already created objects code modules, that it was unclear as to what we would call any variables which we added ( declared ) at the top of those already existent code modules.
    For the cases of our custom objects created from Class modules that we have access to it follows more closely OOP concepts, at least in the case of declaring it as Public
    A variable declared at the top of the text of the Class module will be a variable in the actual code module created using the Class module template. This can easily be related to general OOP concepts. We might decide that are Class is the blueprint for a car. A variable could be the car color.
    So for example, we insert a Class module, giving it the name of Car , and adding a Public variable,
    In Class module, name Car
    RightClickInsertClassModule.JPG :
    NameInsertedClassModule Car.jpg :

    Code:
    Public CrColor As String
    ClsCarColor.jpg : https://imgur.com/NKiwsVt http://i.imgur.com/NKiwsVt.jpg
    ClsCarColor.JPG
    That text of a code line above is to be put into a Class module which you have given the name Car

    We would now be able do normal VBA OOP type programming in a code module to instantiate an instance of a car, that is to say, Dim ( declare ) an object variable to that Class, and then use the Property CrColor of that object variable.
    So this following actual coding is to be put in any code module, not in a Class module– we cannot put this normal running code in a Class module because, as we have learnt, a class module just has the text of coding, but is not actually any coding that we can use.
    In any 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" from the  CrColor  variable in objCr
    End Sub
    Often the simple variable so used is referred to as read and write type to distinguish it from two special ways of doing the same thing as the last simple coding. The special way , ( special Class Procedures text ), is discussed below. It basically does what the last simple coding does but in a way that allows you to do a few other things, including to make the variable only read or write from a normal code module.

    Normal Procedures from Normal procedure text in Class Module
    It is very likely that Normal procedures work within the object code module made from the text in the Class module. We can’t prove this , since we do not have access to the object code module. But if we write normal procedures as text in the Class module, then we find that the procedure appears as a Method of the instantiated object such that we can use it exactly as we did for the discussed in the sections above , “Coding in the object module ran from outside the module
    (Methods)”
    Just to be clear what we are talking about here: We put text in the form of a normal procedure into the Class module. We then instantiate a variable to be an instance of that class. This gives us an object code module ( which we cannot see ) in which a copy of that procedure coding is present. The result of this is that the procedure appears to be a method of the instantiated object.
    For Example
    This text should be put in the Class module we made in the last section, Car:
    Code:
    Public Sub SayHello()
     MsgBox Prompt:="Hello from the object code module which was made from the code text in the template Class module named  Car"
    End Sub
    ClsCarHelloProcedure.JPG : https://imgur.com/Z5H1G91 http://imgur.com/Z5H1G91.jpg
    ClsCarHelloProcedure.JPG

    We can now use the “say hello method” in our last macro in the normal code module. ( We will notice also that it is added to intellisense ( HelloProcedureOfferedInIntellisense.JPG : https://imgur.com/a5ofRio http://i.imgur.com/a5ofRio.jpg )
    This following macro should be put in any normal code module
    Code:
    Sub MyCarColor() '  https://excelfox.com/forum/showthread.php/2451-Class-Stuff-VBA-Custom-Classes-amp-Objects-Class-Modules?p=13066&viewfull=1#post13066
    Dim objCr As Car: Set objCr = New Car ' This is the normal codelines used to typically make an object variable of a particular  Class
    objCr.SayHello
     
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     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
    
    The first thing that the above procedure will do on running is give you a message box which comes from a procedure, Sub SayHello() , which is in the object code module of objCr
    HelloProcedureranfromHiddenObjCodeModule.JPG : https://imgur.com/fDl4xno http://i.imgur.com/fDl4xno.jpg
    HelloProcedureRanfromHiddenObjCodeModule.JPG
    ( We cannot see that object code module from which the procedure is run: Just to remind ourselves again: Microsoft have decide not to let us see that object code module, objCr. ( Microsoft have decided the opposite for the Worksheets and ThisWorkbook object code modules: In those cases which we previously considered, we were able to see and manipulate the object code module, but we could not see the Class module with the blueprint code text from which the objects had been made. ) )


    Private variable text in Class modules.
    Variables defined in this way, ( as Private or just Dim ) in the text at the top of a Class module behave as might be expected in an instantiated object, just as variable only available in that object code module. Such declared variables are used a lot in conjunction with other procedures in the object code modules made from the Class module text.
    For example, we might want to have a specific constant for use later by coding in the object code module taken from the Class module code text. The value of the constant could be filled , for example by the event macro which works automatically when an object is made from the class text.
    For example:
    Lets say we want to have a variable to hold the value the time at which an object is made using the Class module blueprint.
    We add in the text at the top of the Class module a line of text which will appear as a code line at the top of the code module of any object made from the Class template.
    Private TimeStamp As Date
    Within the event procedure, Private Sub Class_Initialize() ( ' ClassCar Sub Class_Initialize().JPG : http://i.imgur.com/xe5Oj2w.jpg ) we can put a text which in the object using this text as a code line will fill the Time Stamp variable,
    Code:
    Private Sub Class_Initialize()
     Let TimeStamp = Now()
    End Sub
    As an example demo of how we might then use this, we could add a line to the text in the Class module related to the procedure that will become a method to any object made from the Class module text template. This line could organise a second message box to tell us the time at which an object had been made.
    So finally, the complete text in the Class module template would look something like this:
    Code:
    Public CrColor As String
    Private TimeStamp As Date
     Private Sub Class_Initialize()    ' ClassCar Sub Class_Initialize().JPG :   http://i.imgur.com/xe5Oj2w.jpg    
    Let TimeStamp = Now()
    End Sub
    Public Sub SayHello()
     MsgBox Prompt:="Hello from the object code module which was made from the code text in the template Class module named  Car" & vbCr & vbLf & "The procedure,  Sub SayHello()  is running from within an object code module which we cannot see"
     MsgBox Prompt:="The object made from class  Car  was made at   " & TimeStamp
    End Sub
    Our last example macro in a normal code module would stay the same:
    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
     objCr.SayHello
     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
    
    On running the above macro, a second message produced from the .SayHello method box would now come up:
    Time Stamp Messsage Box.JPG : https://imgur.com/gIPSeps http://i.imgur.com/gIPSeps.jpg
    Time Stamp Messsage Box.JPG

    Lets just review what we did
    The Class module is really just a template full of text. ( It has a name to distinguish it from other class modules. We chose Car in our current example). That text is all copied to an actual code module, ( which we can’t see) , after a typical instantiating process in a normal code module of this form:
    Dim objCr As Car: Set objCr = New Car
    We chose the object variable name, objCr , arbitrarily. This name would be comparable to the Code Names of the objects already created for us by Microsoft: ThisWorkbook , Sheet1, Sheet2 , Sheet3 …. Etc.
    The coding that already is placed in our , ( not visible to us) object code module , will look exactly like that in the Class text module template. Immediately the object code module is made, the Private Sub Class_Initialize() kicks in and fills the variable TimeStamp with the current date and time. That value in the variable stays constant as long as any macro bringing the objCr object into life is running. If at any point in such a macro we use the method SayHello(), via the command objCr.SayHello , then the procedure inside the ( not visible to us) object code module , Public Sub SayHello() , will run, and at that time the value inside the variable TimeStamp will be used in the following code line to tell us the time at which the object , objCr was brought into life:
    MsgBox Prompt:="The object made from class Car was made at " & TimeStamp







    Attached Files Attached Files
    Last edited by DocAElstein; 03-03-2021 at 05:50 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
  •