Creating Classes & ActiveX Controls

Constants and Enums

You may have noticed by now that you cannot have a public constant in a user control or class module. You may have also noticed that a lot of parameters in other controls come up with a list of possible values, such as vbCritical for the Icon property of the Msgbox function. To do this in VB, you use Enum statements. These are also the only way to create public constants. Here is a sample Enum:

Public Enum ShowDialogConstants
    SDC_ShowOpen = 0
    SDC_ShowSave = 1
    SDC_ShowPageSetup = 2
End Enum

The assignment of values is optional. In order to use this in a parameter, you specify As ShowDialogConstants instead of the usual As Long etc. Then, when the user of your control reaches that parameter of the function, he/she will get a list of possible constants (listed above), instead of having to remember the actual numbers.

Public Sub ShowDialog(vMethod As ShowDialogConstants)
    Select Case vMethod
    Case SCD_ShowOpen
        '// show open
    Case 1 '// the specified numbers can be used too
        '// show save
    ...
End Sub

So, when you come to fill the parameters, you get this:

enums_example.gif (3363 bytes)

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”