Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the Attribute keyword do in VB6?

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.

like image 729
Brian Hooper Avatar asked Nov 11 '15 10:11

Brian Hooper


1 Answers

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.

like image 79
RubberDuck Avatar answered Sep 23 '22 05:09

RubberDuck