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 :
ClsCarColor.jpg : https://imgur.com/NKiwsVt http://i.imgur.com/NKiwsVt.jpgCode:Public CrColor As String
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
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.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
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:
ClsCarHelloProcedure.JPG : https://imgur.com/Z5H1G91 http://imgur.com/Z5H1G91.jpgCode: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
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
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 objCrCode: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
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,
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.Code:Private Sub Class_Initialize() Let TimeStamp = Now() End Sub
So finally, the complete text in the Class module template would look something like this:
Our last example macro in a normal code module would stay the same: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
On running the above macro, a second message produced from the .SayHello method box would now come up: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
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




Reply With Quote
Bookmarks