Reputation: 2014
Why can a form have a public enum block if it cannot have a public constant declaration?
Also if I have a public variable in the declaration section of a form I can reference it throughout the application but I MUST use dot notation e.g form1.var1
However if I put an enum block in the declaration of a form I can reference it throughout the rest of the application but only if it's NOT prefixed with the dot notation.
Are enum blocks the only structure on a form that can only or must only be referenced from outside without dot notation?
What is the logic to this that I am missing?
Upvotes: 5
Views: 2113
Reputation: 5689
The reason for this behaviour is due to VB6 being underlied by COM. The majority of VB types are based on those available to a COM type library (which are contained in all VB components, and most general "ActiveX" components). Public VB Enum statements are equivalent to the "Enum" found in a type library. However, in type libraries, Enums are top level objects (other objects include Interface, CoClass, Module, Record, Union and Alias). This means that VB accesses them as <ProjectName>.<Enum Name>, and this convention is applied to enumerated types created internally. The VB object browser is misleading when it says Form1.MyEnum - that is just saying where it is declared.
As for constants - unfortunately this is functional hole in VB6. COM type libraries do support constants as part of a type library Module, but this ability was never added to VB6 (possibly because VB doesn't have a concept of a type library Module).
Upvotes: 4
Reputation: 244991
The logic here is that an enum defines a type.
You already know about types because you use them everywhere. A class is a type. So is an Integer
, a Long
, a String
, and all of the other built-in data types. And you can create your user user-defined types using the Type
keyword; e.g.,
' Defines a new type User
Type User
Name As String
ID As Integer
PhoneNumber As String
End Type
If you think about it, you'll see that this makes sense. You never use an enum directly. Rather, you use it as a type. You declare variables that hold a value of that enum's type, the same way as you would declare a variable that holds a value of type Integer
or String
.
By contrast, a constant is not a type. It is just a regular value, no different than if you had declared a regular variable, except that a constant variable's value cannot be modified.
Types can be defined anywhere, inside or outside of a class. Variables, however, must either be defined inside of a class or inside of a module.
Upvotes: 4