VB中的类与模块 [英] Class vs. Module in VB

查看:138
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆