Custom Classes and Custom objects. General and UserForms
General Class / Class module use
The Class Module is a Blueprint/Template and the coding in it isn’t coding.
We are now right at where people start:
Right mouse click in the left hand side project explorer window near the representation of your workbook and select something like --- Insert --- Class Module
Add a Class module.jpg : https://imgur.com/X2DdZqY : http://i.imgur.com/X2DdZqY.jpg
Add a Class module.JPG
If you now double click on the Class1 representation that appears in the VB Editor, a large window appears similar to those code windows already discussed. Although we begin now to discus adding coding into this new window, we are in reality, using similar ideas and concepts to define how the coding in an instantiated object from the Class Blueprint/ template performs. It is convenient to use what looks like coding , because objects made from the template/Blueprint will perform as if such coding is running. The “coding” in the Class Module is actually the instructions / Blueprint / template showing exactly how the coding in the instantiated object modules would look like if we could see them as we can the ThisWorkbook and the Workbooks code modules discussed already.
A Class module is not really a code module,
Why by default you can’t step through Class modules in debug ( F8 ) mode
The fact that what you see is not actual coding, but a Blueprint for coding to use, explains why , by default, stepping through a coding manually in debug ( F8 ) mode, does not step through a Class module. What should happen is that it steps through the coding in an object model at the same level in the OOP hierarchy as the ThisWorkbook and the Workbooks code modules discussed already. The problem is that those actual object code modules do not exists. However the coding in those objects must mirror exactly the Template of the Class module, so VBA gives the option to allow the stepping through a coding manually in debug ( F8 ) mode to go through the class module as if it were the actual coding:
Tools Options.JPG : https://imgur.com/KZUyPnB http://i.imgur.com/KZUyPnB.jpg
Break in class modules.JPG : https://imgur.com/75Eg6UE http://i.imgur.com/75Eg6UE.jpg
Having said this, there could be some logical justification for organising the Class module as it is, since possibly it reflects the coding hidden behind many things which make Excel work…
Organisation of and using the Class Module
We are at a point where people first start…
The form of the text that can go in the Class Module is predefined. We may be able to put some text / code text in of some arbitrary form, but it will not be our choice if they do anything like we want. As noted, the text that is of any practical use will look like coding since it represents the coding inside the object code module which we can not see.
Event Coding.
The physical structure of the class code module, that is to say, as it appears on the screen to us, has a similar form to the two object modules already discussed. We only have a couple of Event macros already there
ClassEventMacros.JPG : https://imgur.com/DvLfX2G http://i.imgur.com/DvLfX2G.jpg
ClassEventMacros.JPG
They are fairly explanatory: They do things when we make or break an object…..
Just to remind us… We can’t do anything with the text , ( or coding as it appears to look ) , in the class module since it is just a set of instructions/ template for how actual code within an object made from it will look. So we need to instantiate, that is to say, bring into existence, an object made from this template: The stuff in the Class module id the template from which an actual object code module will be made. VBA does not let us see those modules
If we add text code lines within these two procedures, then in an instantiated object, the actual code lines of those forms , will be done at the instantiation, and at the termination of that object. ( The termination would typically be done in a code line in another module of the form Set Obj = Nothing. ( The instantiation/ bringing into life of the object we have already discussed is done by the typical initial Dim / Seting of the object variable, Obj ) )
This is a very important concept that most people, including a lot of computer experts don’t understand, so I will say it again:
The text lines looking like code in the Class module are not real code. They represent what the actual code lines in an object code module of an object made from the class will look like. But we will never see those code lines. The makers of Excel decided to prevent us from seeing them. In the case of the two things already discussed ( worksheet object code modules and the ThisWorkbook object code module ) we had the opposite situation: We could see and manipulate the actual object code modules, but we could not see the Class modules used as the template from which to make those. We are guessing that somehow when we start excel, a hidden class module is used as a template to create the ThisWorkbook object code module , and possibly for each of the worksheets that we see, there is a similar class module which we can’t see, which is used as a template to create those.




Reply With Quote
Bookmarks