VB中的类与模块 [英] Class vs. Module in VB
问题描述
有什么优势,如果有的话,使用模块而不是VB中的类?它们如何不同,使用模块有什么优点/缺点?在VB或VB.NET中,我同时使用。
(A) > 和 (B)只有 解决了同样的问题:两者都允许您逻辑地组一组函数。 使用模块的优点: 使用类的优点具有共享函数:
共享的类
功能
(=非共享)变量,属性。
因此,如果您正在编写一组辅助函数并希望对它们进行逻辑分组 这个组没有意义),使用模块 - 这正是他们在这里。另一方面,如果你有一个概念上适合已经存在的类的函数,将它作为一个共享函数添加到该类中。
What advantage is there, if any, to using Modules rather than classes in VB? How do they differ, and what advantages/disadvantages are there in using modules? In VB or VB.NET, I use both.
(A) Modules
and
(B) Classes with only Shared
functions
solve the same problem: Both allow you to logically group a set of functions.
Advantages of using a module:
- It allows you to define extension methods.
- For someone reading your code, it is immediately obvious that this is not a class representing a group of stateful objects but just a "function container".
Advantages of using a class with shared functions:
- It's easy to extend it with instance (= non-shared) variables, functions and properties later on.
So, if you are writing a set of helper functions and want to logically group them (where the concept of a state of this group just doesn't make sense), use a module -- this is exactly what they are here for. On the other hand, if you have a function that conceptually fits to an already existing class, add it as a shared function to that class.
这篇关于VB中的类与模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!