Reputation: 3850
when should I go for a module and when for a class?
when module and class are loaded onto memory?
Can I unload the module and class already loaded?
Upvotes: 0
Views: 91
Reputation: 13267
A static (BAS) module loads and stays resident for the duration of the run. A class (CLS) module provides for more dynamic allocation of both code and data.
Classes also offer better encapsulation, can have multiple instances created, can be persisted, and have many other advantages over static allocation.
There is a whole section in the manual named "Programming With Objects" you might want to read and study. All legitimate VB6 Editions above the Learning Edition should have this material and more in the MSDN CDs that ship with them. The hardcopy books can also still be found from some new and used sources.
Upvotes: 4
Reputation: 5689
For each BAS module, all module level variables are allocated when the application starts up. You cannot deallocate these variables (although you can set object references to Nothing, the actual variable will still exist).
A CLS module's module level variables are only allocated when the class is instantiated. All memory allocated for an instance of the class is deallocated when the class is destroyed. You can create as many instances of the CLS module as you want, and each has its own private set of module-level variables.
In BAS modules, the scope of variables declared Public is global to the application. However, in CLS modules, you can only access a Public variable if you have a reference to an instance of that class (behind the scenes that variable becomes a Public Property).
In general, all variables and routines that you want to be accessed from any module should be put into a BAS module.
Upvotes: 0