Organisation of Public Constants. (1 Viewer)

Status
Not open for further replies.

ChrisO

Registered User.
Local time
Tomorrow, 09:28
Joined
Apr 30, 2003
Messages
3,202
Organisation of Public Constants.

Keeping track of Public Constants can be difficult.
They do not lend themselves to easy organisation and we can not use Intellisense to display them.

The method presented here is to create small class modules to contain related constants.
We can then combine those related class modules into a single Constants class module.

For example:-

Class 1:
Class clsConstFill …
Code:
Option Explicit
Option Compare Text

Public SOLID       As Long
Public TRANSPARENT As Long

Private Sub Class_Initialize()

    SOLID = 0
    TRANSPARENT = 5
    
End Sub

Class 2:
Class clsConstPen…
Code:
Option Explicit
Option Compare Text

Public PS_SOLID       As Long
Public PS_DASH        As Long
Public PS_DOT         As Long
Public PS_DASHDOT     As Long
Public PS_DASHDOTDOT  As Long
Public PS_NULL        As Long
Public PS_INSIDEFRAME As Long

Private Sub Class_Initialize()

    PS_SOLID = 0
    PS_DASH = 1
    PS_DOT = 2
    PS_DASHDOT = 3
    PS_DASHDOTDOT = 4
    PS_NULL = 5
    PS_INSIDEFRAME = 6
    
End Sub

Class 3:
Class clsConstVbColours…
Code:
Option Explicit
Option Compare Text

Public BLACK   As Long
Public RED     As Long
Public GREEN   As Long
Public YELLOW  As Long
Public BLUE    As Long
Public MAGENTA As Long
Public CYAN    As Long
Public WHITE   As Long

Private Sub Class_Initialize()

    BLACK = vbBlack
    RED = vbRed
    GREEN = vbGreen
    YELLOW = vbYellow
    BLUE = vbBlue
    MAGENTA = vbMagenta
    CYAN = vbCyan
    WHITE = vbWhite
    
End Sub


Now combine the Classes:
Class clsConstants…
Code:
Option Explicit
Option Compare Text

Public Fill   As New clsConstFill
Public Pen    As New clsConstPen
Public Colour As New clsConstVbColours

Private Sub Class_Terminate()
    Set Fill = Nothing
    Set Pen = Nothing
    Set Colour = Nothing
End Sub


Usage:
Behind a Form…
Code:
Option Explicit
Option Compare Text

[color=green]' Add one Class Constants.[/color]
Public Constants As New clsConstants

Private Sub Form_Open(ByRef intCancel As Integer)
    Me.Section(acDetail).BackColor = Constants.Colour.YELLOW
End Sub

Private Sub cmdSetDetailToRed_Click()
    Me.Section(acDetail).BackColor = Constants.Colour.RED
End Sub

Note in the above Form code:
Class Constants is instantiated when the Form is opened.
The word ‘Constants’ must be typed manually followed by a period.
A list of Constants members is produced; Colour, Fill and Pen.
Select Colour followed by a period.
A list of Colour members is produced.
Select the Colour Constant required, in this case; YELLOW or RED.

The use of upper case for the resulting constant is not mandatory.
However, it does give a clue when we hit the end CONSTANT and it also complies with constants used in API calls.
So, if we CamelCase up to the final CONSTANT we know then we are at the end of the selection process.

It follows that we can also pre-class things like Fill with a parent class such as Line, Circle (whatever) that relates the Fill constants to a particular parent class.

I guess it should be pointed out that, strictly speaking, these are not really Constants, they can be written too.

If there are any questions, please ask them in the appropriate forum.

Regards,
Chris.
 
Last edited:
Status
Not open for further replies.

Users who are viewing this thread

Top Bottom