Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vb6

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

like image 921
Chandralal Avatar asked Jul 06 '15 19:07

Chandralal


People also ask

What is the difference between module and class module?

A class is more of a unit, and a module is essentially a loose collection of stuff like functions, variables, or even classes. In a public module, classes in the project have access to the functions and variables of the module. You don't have to specify the module name to address one.

What is difference between class module and form module in VB?

Actually, form are just class module that have control placed on form and can we display form windows. Class modules do not have visual component as compare to form module. Standard module: – Standard module are container for procedure and declaration, commonly accessed by other module with in the application.

What is module explain different types of modules in VB?

Edit Article. Modules are containers to define custom functions, procedures or variables to group code in Visual Basic. Module containing an entry point subroutine (main) is an entry module. It is always at least one module defined in the Visual Basic macro.

What is class module in vb6?

A class module encapsulates the data within 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 within a standard module are visible anywhere in the project.


1 Answers

A Module(.bas) has methods and variables that can be used globally in your program and there is only a single instance of the data (similar to a Static method or field in C#). A Class Module(.cls) has properties and methods that usually can only be accessed when the object is instantiated, but can have multiple copies, each with differing data.

From MSDN: Visual Basic Concepts:

Classes differ from standard modules in the way their data is stored. There is never more than one copy of a standard module’s data. This means that when one part of your program changes a public variable in a standard module, and another part of your program subsequently reads that variable, it will get 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) vs. Module(.bas):

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.

like image 139
C-Pound Guru Avatar answered Oct 16 '22 18:10

C-Pound Guru