Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Jun 4, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Class Module(.cls) vs. Module(.bas)

For the same global function, what's the difference between placing it in a global module and in a class module? Are there any performance issues involved? If so, which way is recommended and better?

Deciding between a standard module and a class module is not a decision based on performance, but one of design. The main difference between the two is in the way that they handle data. A standard module stores only one copy of the data. A class module encapsulates the data within each instance of the class. That is, for each instance of the class, the data exists separately.

The other main difference is the scope of variables and procedures within the module. In general, any variables and procedures declared as Public within a standard module are visible anywhere in the project or external programs if the standard module is in a component. Variables and procedures declared as Public within a class module can only be seen through a reference to an instance of the class module.

The lifetime of data and procedures stored within a module is affected by which type of module is used. The lifetime of the data and procedures in a class module is defined by the lifetime of the object. So data and procedures are available only if a reference to the object exists. Data and procedures declared within standard modules are available for the lifetime of the program.

Therefore, to answer your question, if you are writing a function that you want available throughout the lifetime of the program and visible to all code in the application, then place the function within a standard module.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date