VB.NET 中的类与模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/881570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Classes vs. Modules in VB.NET
提问by Tom Juergens
Is it considered an acceptable practice to use Modules instead of Classes with Shared member functions in VB.NET?
在 VB.NET 中使用模块而不是具有共享成员函数的类是否被认为是可接受的做法?
I tend to avoid Modules, because they feel like leftover remains from Visual Basic 6.0 and don't really seem to fit in anymore. On the other hand, there doesn't seem to be much difference between using a Module and a Class with only Shared members. It's not that often that I really have much need for either, but sometimes there are situations where they present a simple solution.
我倾向于避免使用模块,因为它们感觉像是 Visual Basic 6.0 的剩余部分,并且似乎不再适合。另一方面,使用只有共享成员的模块和类之间似乎没有太大区别。我并不经常需要它们,但有时它们会提供简单的解决方案。
I'm curious to hear whether you have any opinion or preferences one way or the other.
我很想知道您是否有任何意见或偏好。
回答by Mehrdad Afshari
Module
s are VB counterparts to C# static
classes. When your class is designed solely for helper functions and extension methods and you don'twant to allow inheritanceand instantiation, you use a Module
.
Module
s 是 C#static
类的VB 对应物。当你的类仅设计用于辅助功能和扩展方法,你不希望允许继承和实例,您可以使用Module
。
By the way, using Module
is not really subjective and it's not deprecated. Indeed you must use a Module
when it's appropriate. .NET Framework itself does it many times (System.Linq.Enumerable
, for instance). To declare an extension method, it's required to use Module
s.
顺便说一句,使用Module
并不是真正主观的,也没有被弃用。实际上,您必须Module
在适当的时候使用 a 。.NET Framework 本身做了很多次(System.Linq.Enumerable
例如)。要声明扩展方法,需要使用Module
s。
回答by dr. evil
I think it's a good idea to keep avoiding modules unless you stick them into separate namespaces. Because in Intellisense methods in modules will be visible from everywhere in that namespace.
我认为最好避免使用模块,除非您将它们放入单独的命名空间中。因为在 Intellisense 模块中的方法将在该命名空间的任何地方都可见。
So instead of ModuleName.MyMethod()
you end up with MyMethod()
popups in anywhere and this kind of invalidates the encapsulation. (at least in the programming level).
所以,而不是ModuleName.MyMethod()
你最终MyMethod()
在任何地方弹出窗口,这种使封装无效。(至少在编程层面)。
That's why I always try to create Class with shared methods, seems so much better.
这就是为什么我总是尝试使用共享方法创建类,看起来好多了。
回答by JaredPar
Modules are by no means deprecated and are used heavily in the VB language. It's the only way for instance to implement an extension method in VB.Net.
模块绝不被弃用,并且在 VB 语言中被大量使用。例如,这是在 VB.Net 中实现扩展方法的唯一方法。
There is one huge difference between Modules and Classes with Static Members. Any method defined on a Module is globally accessible as long as the Module is available in the current namespace. In effect a Module allows you to define global methods. This is something that a class with only shared members cannot do.
具有静态成员的模块和类之间存在巨大差异。只要模块在当前命名空间中可用,在模块上定义的任何方法都可以全局访问。实际上,模块允许您定义全局方法。这是只有共享成员的类无法做到的。
Here's a quick example that I use a lot when writing VB code that interops with raw COM interfaces.
这是我在编写与原始 COM 接口互操作的 VB 代码时经常使用的一个快速示例。
Module Interop
Public Function Succeeded(ByVal hr as Integer) As Boolean
...
End Function
Public Function Failed(ByVal hr As Integer) As Boolean
...
End Function
End Module
Class SomeClass
Sub Foo()
Dim hr = CallSomeHrMethod()
if Succeeded(hr) then
..
End If
End Sub
End Class
回答by Chandra Malla
It is acceptable to use Module
. Module
is not used as a replacement for Class
. Module
serves its own purpose. The purpose of Module
is to use as a container for
使用是可以接受的Module
。Module
不用作Class
. Module
服务于它自己的目的。的目的Module
是用作容器
- extension methods,
- variables that are not specific to any
Class
, or - variables that do not fit properly in any
Class
.
- 扩展方法,
- 不特定于 any 的变量
Class
,或 - 不适合任何
Class
.
Module
is not like a Class
since you cannot
Module
不像 aClass
因为你不能
- inherit from a
Module
, - implement an
Interface
with aModule
, - nor create an instance of a
Module
.
- 继承自
Module
, Interface
用 a实现一个Module
,- 也不创建 a 的实例
Module
。
Anything inside a Module
can be directly accessed within the Module
assembly without referring to the Module
by its name. By default, the access level for a Module
is Friend
.
Module
可以在Module
程序集中直接访问a 中的任何内容,而无需Module
通过其名称引用。默认情况下,对于一个访问级别Module
是Friend
。
回答by Suji
Classes
班级
- classes can be instantiated as objects
- Object data exists separately for each instantiated object.
- classes can implement interfaces.
- Members defined within a class are scoped within a specific instance of the classand exist only for the lifetime of the object.
- To access class members from outside a class, you must use fully qualified namesin the format of Object.Member.
- 类可以实例化为对象
- 每个实例化对象的对象数据分别存在。
- 类可以实现接口。
- 在类中定义的成员的作用域在类的特定实例内,并且仅在对象的生命周期内存在。
- 要从类外部访问类成员,必须使用Object.Member格式的完全限定名称。
Modules
模块
- Modules cannot be instantiated as objects,Because there is only one copy of a standard module's data, when one part of your program changes a public variable in a standard module, it will be visible to the entire program.
- Members declared within a module are publicly accessibleby default.
- It can be accessed by any code that can access the module.
- This means that variables in a standard module are effectively global variables because they are visible from anywhere in your project, and they exist for the life of the program.
- 模块不能被实例化为对象,因为标准模块的数据只有一份副本,当你的程序的一部分改变了标准模块中的公共变量时,它对整个程序都是可见的。
- 默认情况下,在模块中声明的成员是可公开访问的。
- 任何可以访问模块的代码都可以访问它。
- 这意味着标准模块中的变量实际上是全局变量,因为它们在项目的任何地方都可见,并且在程序的整个生命周期内都存在。
回答by JRS
When one of my VB.NET classes has all shared members I either convert it to a Module with a matching (or otherwise appropriate) namespace or I make the class not inheritable and not constructable:
当我的 VB.NET 类之一具有所有共享成员时,我要么将其转换为具有匹配(或其他适当)命名空间的模块,要么使该类不可继承且不可构造:
Public NotInheritable Class MyClass1
Private Sub New()
'Contains only shared members.
'Private constructor means the class cannot be instantiated.
End Sub
End Class
回答by GGSoft
Modules are fine for storing enums and some global variables, constants and shared functions. its very good thing and I often use it. Declared variables are visible acros entire project.
模块非常适合存储枚举和一些全局变量、常量和共享函数。它非常好,我经常使用它。声明的变量在整个项目中都是可见的。
回答by SteveCinq
You mustuse a Module (rather than a Class) if you're creating Extension methods. In VB.NET I'm not aware of another option.
你必须,如果你正在创建扩展方法使用一个模块(而不是类)。在 VB.NET 中,我不知道另一种选择。
Being resistant to Modules myself, I just spent a worthless couple of hours trying to work out how to add some boilerplate code to resolve embedded assemblies in one, only to find out that Sub New()
(Module) and Shared Sub New()
(Class) are equivalent. (I didn't even know there wasa callable Sub New()
in a Module!)
我自己对模块很抗拒,我只是花了几个毫无价值的小时试图找出如何添加一些样板代码来解析嵌入式程序集,结果发现Sub New()
(Module) 和Shared Sub New()
(Class) 是等效的。(我甚至不知道有是一个可调用Sub New()
的模块!)
So I just threw the EmbeddedAssembly.Load
and AddHandler AppDomain.CurrentDomain.AssemblyResolve
lines in there and Bob became my uncle.
所以我只是把EmbeddedAssembly.Load
和AddHandler AppDomain.CurrentDomain.AssemblyResolve
线扔在那里,鲍勃成了我的叔叔。
Addendum: I haven't checked it out 100% yet, but I have an inkling that Sub New()
runs in a different order in a Module than a Class, just going by the fact that I had to move some declarations to inside methods from outside to avoid errors.
附录:我还没有 100% 地检查它,但我有一个暗示,Sub New()
它在模块中以与类不同的顺序运行,只是因为我不得不将一些声明从外部移动到内部方法以避免错误。