Class Visual Basic 中的模块 (.cls) 与模块 (.bas)
Class Module(.cls) vs Module(.bas) in Visual Basic
Class 模块 (.cls) 和 . Visual Basic 中的模块 (.bas) ?
如果代码在程序的生命周期内需要并且对应用程序中的所有代码可见,则将函数放在标准模块中。
一个标准模块只存储一份数据。 class 模块将数据封装在 class 的每个实例中。也就是说,对于 class 的每个实例,数据都是单独存在的。
通常,在标准模块中声明为 Public 的任何变量和过程在项目的任何位置都是可见的。
在 class 模块中声明为 Public 的变量和过程只能通过对 class 模块实例的引用来查看。
模块中存储的数据和过程的生命周期受使用的模块类型的影响。 class 模块中数据和过程的生命周期由对象的生命周期定义。因此,仅当存在对对象的引用时,数据和过程才可用。在标准模块中声明的数据和过程在程序的生命周期内可用。
模块 (.bas) 具有可在您的程序中全局使用的方法和变量,并且只有一个数据实例(类似于 C# 中的静态方法或字段)。 Class Module(.cls) 具有通常只能在实例化对象时才能访问的属性和方法,但可以有多个副本,每个副本具有不同的数据。
来自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.
来自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.
Class 模块 (.cls) 和 . Visual Basic 中的模块 (.bas) ?
如果代码在程序的生命周期内需要并且对应用程序中的所有代码可见,则将函数放在标准模块中。
一个标准模块只存储一份数据。 class 模块将数据封装在 class 的每个实例中。也就是说,对于 class 的每个实例,数据都是单独存在的。
通常,在标准模块中声明为 Public 的任何变量和过程在项目的任何位置都是可见的。 在 class 模块中声明为 Public 的变量和过程只能通过对 class 模块实例的引用来查看。
模块中存储的数据和过程的生命周期受使用的模块类型的影响。 class 模块中数据和过程的生命周期由对象的生命周期定义。因此,仅当存在对对象的引用时,数据和过程才可用。在标准模块中声明的数据和过程在程序的生命周期内可用。
模块 (.bas) 具有可在您的程序中全局使用的方法和变量,并且只有一个数据实例(类似于 C# 中的静态方法或字段)。 Class Module(.cls) 具有通常只能在实例化对象时才能访问的属性和方法,但可以有多个副本,每个副本具有不同的数据。
来自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.
来自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.