Partager via


Visual Basic Concepts

Providing Named Constants for Your Control

As with other component types, public enumerations can be shared by all of the controls in a control component (.ocx file). Place public Enums for your component in any UserControl code module.

"Providing Named Constants for Your Component," in "General Principles of Component Design," discusses techniques for providing constants, validating constants in properties, and so forth. See that topic for general information on the subject.

There are two additional factors specific to control components:

  • Enum member names are used in the Properties window.

  • Global objects cannot be used to simulate string constants.

Enum Member Names in the Properties Window

As an example of the first factor, consider the following Enum and property:

Public Enum DINOSAUR
   dnoTyrannosaur
   dnoVelociraptor
   dnoTriceratops
End Enum

Private mdnoFavoriteDinosaur As DINOSAUR

Public Property Get FavoriteDinosaur() As DINOSAUR
   FavoriteDinosaur = mdnoFavoriteDinosaur
End Property

Public Property Let FavoriteDinosaur(ByVal NewDino _
      As DINOSAUR
   mdnoFavoriteDinosaur = NewDino
   PropertyChanged "FavoriteDinosaur"
End Property

When you set the FavoriteDinosaur property in the Properties window, the drop down list will contain dnoTyrannosaur, dnoVelociraptor, and dnoTriceratops.

As you can see, there's a fine tradeoff here between names that will look good in the drop down, and names that will avoid collisions with names used in Enums for other components.

As a rule of thumb, don't abandon the prefix ("dno" in the example above) that groups constants in global lists. The prefix provides at least some protection from name conflicts. On the other hand, don't make your prefixes so long that they obscure the names.

Cannot Simulate String Constants Using Global Objects

Class modules in control components can have one of two values of the Instancing property, Private or PublicNotCreatable. The Instancing values that enable global objects are not available in control components, so it is not possible to simulate string constants using properties of global objects, as described in "Providing Named Constants for Your Component" in "General Principles of Component Design."