VB6中的公共财产,朋友和公共变量有什么区别 [英] What is the difference between Public Property, Friend and Public Variable in VB6
问题描述
好的,所以我知道离子VB6,一类封装的属性可以属于以下三类之一:
OK so I understand that ion VB6, encapsulated properties in a class can belong to one of three categories:
- 公共属性
- 朋友
- 公共变量
有什么区别在这些之间以及如何将它们与C#等更现代的语言中的公共财产和私有财产进行比较?
What is the difference between these and how do these compare to public and private properties in a more modern language like C#?
推荐答案
作用域限定符 公共
和朋友
确定不同项目中的客户是否可以看到该项目。
The scope qualifiers Public
and Friend
determine whether clients in different projects can see the item.
-
公共
项可以被其他项目中的客户端代码访问 1 和在同一项目中编码。 -
Friend
项目仅可用于同一项目中的代码,而 不可用于其他项目中的代码。 -
私有
项目仅可用于同一类中的代码。
Public
items will be accessible to client code in other projects1 and code in the same project.Friend
items are accessible only to code in the same project, not to code in other projects.Private
items are accessible only to code in the same class.
属性与公共变量不同,因为使用属性可以在客户端获取或设置值时执行自己的代码2 。 编辑,遵循Deanna的评论:另外请注意,变量可以通过ByRef传递给函数,更改将按预期进行。
Properties are different from public variables, because with properties you can execute your own code when the client gets or sets the value2. EDIT following Deanna's comment: Also note that variables can be passed ByRef to a function and changes will work as expected. This is NOT the case for properties.
NB C#可能更现代,但是恕我直言,对属性和公共变量的VB6处理是比.Net处理要好得多。
NB C# may be more modern, but IMHO the VB6 treatment of properties and public variables is significantly better than the .Net treatment.
- In VB6 you can change a public variable into a property without breaking the clients. You don't even have to recompile them. Not true in .Net.
- In VB6 public variables can be used with data binding. Not true in .Net.
- In VB6 public variables could be used with interfaces. Not true in .Net.
恕我直言,Microsoft在创建.Net属性和公共字段之间的这些差异时犯了一个真正的设计错误。不服气吗? .Net的第一个发行版之后, C#和 VB 编译器已修改为支持自动实现的属性。这些使您可以仅用一行代码创建属性,以便以后可以在获取/设置上添加逻辑而不会引起问题。恕我直言,这证明了应该使公共变量与属性没有区别。
IMHO Microsoft made a real design mistake in creating these differences between properties and public fields in .Net. Not convinced? After the first releases of .Net, the C# and VB compilers were modified to support automatically implemented properties. These allow you to create properties in just one line of code, so that it's later possible to add logic on get/set without causing problems. IMHO this proves that public variables should have been made indistinguishable from properties.
1假设您的项目类型实际上允许您的类供其他项目(例如ActiveX DLL,OCX或ActiveX exe)使用。
2在属性获取$ c $中c>,
Property Let
和 Property Set
过程。
1 Assuming your project type actually allows your classes to be used by other projects (i.e. ActiveX DLL, OCX, or ActiveX exe).
2 In the Property Get
, Property Let
and Property Set
procedures.
这篇关于VB6中的公共财产,朋友和公共变量有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!