属性关键字在 VB6 中有什么作用? [英] What does the Attribute keyword do in VB6?

查看:27
本文介绍了属性关键字在 VB6 中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些 VB6 代码转换为 VB.Net.由于我的 VB6 安装似乎已损坏无法修复,因此我使用记事本阅读原始源代码,并且可以在文件顶部附近看到:-

属性 VB_Name = "clsBulge"属性 VB_GlobalNameSpace = False属性 VB_Creatable = True属性 VB_PredeclaredId = False属性 VB_Exposed = False属性 VB_Description = "这里有一些文本"属性 VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"属性 VB_Ext_KEY = "Member0" ,"collBulges"属性 VB_Ext_KEY = "Top_Level" ,"Yes"

在作品中:-

公共属性让振幅(ByVal vData As Double)属性 Amplitude.VB_Description = "这里有一些文本"mvaInternal = vData最终财产

问题是,在转换为 VB.Net 时,我是否需要担心这一点?如果是这样,我在哪里可以找到所有这些东西的含义?

此处给出的问题和答案表明不是,但不是不是真正的权威来源.一个类似的问题问 在这里很快就变得无关紧要了.

解决方案

我写了一些关于 VBA 上下文中的 VB 属性,但我可以在这里总结一下这些属性.

<小时><块引用>

属性 VB_Name = "clsBulge"

很容易解释,这是类的名称.要创建它的新实例,您必须调用 Dim foo = New clsBulge.

<小时><块引用>

属性 VB_GlobalNameSpace = False

这个有点有趣,通过将其设置为 true,一个全局将创建默认实例. 当应用程序启动时,将自动创建该类的一个实例并可以通过简单的名称访问对其公共成员进行访问.解释起来有点困难,但是如果您查看对象资源管理器中的内置 VBA 函数,您将很快看到它如何允许您快捷地"创建命名空间".

移植时您不必担心这个问题,除非它设置为 True.任何将此设置为 True 的类都会让您头疼,因为此静态"类的客户端不必通过其显式名称来调用它,但在您将代码移植到 .Net 后必须这样做.

<小时><块引用>

属性 VB_PredeclaredId = False

VB_GlobalNameSpace 相关,但语义略有不同.它大致相当于 .Net 中的静态类.只是...不是,因为您仍然可以创建该类的其他实例.它也在以上链接中描述为:

<块引用>

如果类模块的 VB_PredeclaredId 属性或 VB_GlobalNamespace 属性具有值True",则该类模块具有默认实例变量.这个默认实例变量是用模块范围创建的,就像在包含类名的元素中声明一样.

如果该类模块的 VB_PredeclaredId 属性值为True",则该默认实例变量以类名作为其名称.将此命名变量作为 Set 赋值的目标是无效的.否则,如果此类模块的 VB_PredeclaredId 属性不具有值True",则此默认实例变量没有可公开表达的名称.

如果该类模块的 VB_GlobalNamespace 属性值为True",则该类模块被视为全局类模块,允许对其默认实例的成员进行简单的名称访问...

请注意,如果 VB_PredeclaredIdVB_GlobalNamespace 属性都具有值True",则两个属性的语义共享相同的默认实例变量.

<小时><块引用>

属性 VB_Creatable = True

这个也很有意思.它与范围规则有关.本质上,如果将其设置为 True,则可以从任何地方调用它的构造函数.它是公共的,可以从任何地方创建.但是如果设置为False,就相当于拥有了一个Internal ctor.

<小时><块引用>

属性 VB_Exposed = False

简单地控制模块的范围.True 是公共的,False 是内部的.它与 VB_Creatable 结合使用以创建一个矩阵范围界定行为.

<小时><块引用>

Attribute VB_Description = "这里有一些文字"

大致相当于

文档注释.该文本将显示在 VB6(和 VBA)对象浏览器中.如果我没记错的话,它被许多其他支持 COM 的语言用于相同的目的.您实际上可以使用 ComponentModel.Description 属性. 如果您需要您的端口对 COM 可见,您需要使用它以便您的客户保留文档.

<小时><块引用>

属性 VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"属性 VB_Ext_KEY = "Member0" ,"collBulges"属性 VB_Ext_KEY = "Top_Level" ,"Yes"

这些是 IDE 插件使用的自定义属性.我不能具体说这些做了什么,但不太可能需要保留它们.

I am converting some VB6 code to VB.Net. Since my VB6 installation appears to be damaged beyond repair I am using Notepad to read the original source code and can see at near the top of the file:-

Attribute VB_Name = "clsBulge"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Description = "Some text here"
Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
Attribute VB_Ext_KEY = "Member0" ,"collBulges"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"

and down in among the works:-

Public Property Let Amplitude(ByVal vData As Double)
Attribute Amplitude.VB_Description = "Some text here"
    mvaInternal = vData
End Property

The question is, do I have to worry myself about this when converting to VB.Net? If so, where could I find out what all these things mean?

The question and answer given here suggests not, but isn't really an authoritative source. A similar question asked here quickly blathers off into irrelevency.

解决方案

I wrote a bit about VB Attributes in a VBA context, but I can summarize what these are here.


Attribute VB_Name = "clsBulge"

Pretty self explanatory, this is the name of the class. To create a new instance of it, you'll have to call Dim foo = New clsBulge.


Attribute VB_GlobalNameSpace = False

This one is kind of interesting, by setting it to true, a global default instance will be created. When the application starts up, an instance of the class will be automatically created and accessible via simple name access to its public members. It's a little difficult to explain, but if you look at the built in VBA functions in the object explorer, you'll quickly see how this allows you to "shortcut" a "namespace".

You don't have to worry about this one when porting unless it's set to True. Any classes where this is set to True will give you a headache because clients of this "static" class didn't have to call it by its explicit name, but will have to after you've ported your code to .Net.


Attribute VB_PredeclaredId = False

Related to VB_GlobalNameSpace, but with slightly different semantics. It's roughly equivalent to a Static class in .Net. Only... not, because you can still create other instances of the class. It's also described in the link above as:

A class module has a default instance variable if its VB_PredeclaredId attribute or VB_GlobalNamespace attribute has the value "True". This default instance variable is created with module extent as if declared in a containing an element whose was the name of the class.

If this class module’s VB_PredeclaredId attribute has the value "True", this default instance variable is given the name of the class as its name. It is invalid for this named variable to be the target of a Set assignment. Otherwise, if this class module’s VB_PredeclaredId attribute does not have the value "True", this default instance variable has no publicly expressible name.

If this class module’s VB_GlobalNamespace attribute has the value "True", the class module is considered a global class module, allowing simple name access to its default instance’s members...

Note that if the VB_PredeclaredId and VB_GlobalNamespace attributes both have the value "True", the same default instance variable is shared by the semantics of both attributes.


Attribute VB_Creatable = True

This one is also interesting. It has to do with scoping rules. Essentially, if this is set to True, it's constructor can be called from anywhere. It's Public and can be created from anywhere. But if it's set to False, it's equivalent to having an Internal ctor.


Attribute VB_Exposed = False

Simply controls the scope of the module. True is Public, False is Internal. It's used in combination with VB_Creatable to create a matrix of scoping behavior.


Attribute VB_Description = "Some text here"

Roughly equivalent to a <Summary> doc comment. This text will show up in the VB6 (and VBA) object browser. If I recall correctly, this is used by many other COM capable languages for the same purpose. You can actually produce this exact behavior for your COM exposed .Net libraries by using the ComponentModel.Description attribute. If you need your port to be COM visible, you'll want to use it so that your clients keep the documentation.


Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
Attribute VB_Ext_KEY = "Member0" ,"collBulges"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"

These are custom attributes used by IDE Add-Ins. I can't specifically say what these did, but it's unlikely they need to be preserved.

这篇关于属性关键字在 VB6 中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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