What does the Attribute keyword do in VB6?
Asked Answered
R

1

8

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.

Runty answered 11/11, 2015 at 10:36 Comment(2)
Why do you think you’d need to worry about this? They’re part of a standard VB6 file and the converter will know how to handle them. I’m not sure what kind of authoritative source you’d need for such a statement.Prescribe
It isn't fundamentally different from attributes in .NET. Almost all of these are necessary because VB6 generates COM code, these attributes determine what ends up in the type library. Pretty unlikely you'll care about COM when you translate to .NET.Claqueur
A
14

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.

Affable answered 9/1, 2016 at 0:10 Comment(3)
Looks like the ext attributes were metadata added by the VB6 class builder, a GUI tool that generates classes and their members. Not an add-in, AFAIK it's part of the VB6 IDE. But yeah, add-ins could be using these too.Photic
This is a great list & very useful answer. Do you know if this is a comprehensive list of attributes?Anthony
I don’t know if it’s comprehensive, but between the blog & this answer, it’s close @DaveInCaz. It’s a shame SO shut down docs. The community had put together some pretty comprehensive examples there. Maybe you could find the archive.Affable

© 2022 - 2024 — McMap. All rights reserved.