Class Module (.cls) vs Module (.bas) in Visual Basic

What is the difference between class module (.cls) and. Module (.bas) in Visual Basic?

+7
vb6
source share
2 answers

A module (.bas) has methods and variables that can be used globally in your program, and there is only one instance of data (similar to a static method or field in C #). A class module (.cls) has properties and methods that can usually be accessed only when creating an instance of an object, but can have multiple copies, each with different data.

From MSDN: Visual Basic Concepts :

Classes differ from standard modules in how their data is stored. There is no more than one copy of the standard module data. This means that when one part of your program changes a public variable to a standard module, and the other part of your program subsequently reads this variable will receive the same value.

Class module data, on the other hand, exists separately for each instance of the class.

And from Devx.com: class module (.cls) versus module (.bas) :

The solution between the standard module and the class module is not a performance based solution, but one of design. The main difference between the two is that they process the data. The standard module stores only one copy of the data. The class module encapsulates the data in each instance of the class. That is, for each instance of the class, the data exists separately.

Another major difference is the scope of the variables and procedures in the module. In general, any declared variables and procedures since publication in the standard module is visible anywhere in the project or external programs if the standard module is in the component. Variables and procedures declared as Public in the class module can only be seen through a link to an instance of the class module.

The lifetime of the data and procedures stored in the module depends on which type of module is used. The data lifetime and procedures in the class module are determined by the lifetime of the object. Thus, data and procedures are available only if an object reference exists. Data and procedures declared in standard modules, available for program life.

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

+4
source share

If the code is necessary for the lifetime of the program and is displayed for all the code in the application, then place the function in the standard module.

The standard module stores only one copy of the data. The class module encapsulates data in each instance of the class. That is, for each instance of the class, the data exists separately.

In general, any variables and procedures declared as Public in the standard module are visible anywhere in the project. Variables and procedures declared as Public in the class module can only be visible through a link to an instance of the class module.

The lifetime of the data and procedures stored in the module depends on what type of module is used. The lifetime of the data and procedures in the class module is determined by the lifetime of the object. Thus, data and procedures are available only if there is a reference to the object. The data and procedures announced in the standard modules are available for the life of the program.

0
source share

All Articles