What does the Attribute keyword do in VB6?

时光毁灭记忆、已成空白 提交于 2019-12-03 15:20:55
RubberDuck

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!