Tuesday, August 15, 2006

Constants

Good programming practices suggest the use of compile-time constants rather than literal strings.

Instead of writing:
strWorkFile = strWorkFile & ".TXT"

We write:

strWorkFile = strWorkFile & strcExtentTxt


And include a declaration:

Public Const strcExtentTxt As String = ".TXT"


This may seem like extra work - replacing one line of code with two lines, but think again.

By placing the character string in a constant and using the identifier for reference, we are assured of a common and consistent definition of the string, and have avoided the costly maintenance problem of a typo in one of perhaps twenty occurrences of the string throughout or program.

As well, we can convert the compile-time constant to an installation or a run-time constant with a small modification, and will not be required to search through the program looking for occurrences of the literal string.

You can draw on many years of established consistency by using the string constants defined in UW.DOT.

Use Intellisense to draw out identifiers that are prefaced with "strc" (as in "str"ing "c"onstant)
Sub test()
MsgBox UW.strcAlpha
MsgBox UW.strcAlphaDigits
MsgBox UW.StrcDigits
MsgBox UW.strcDriveSeparator
End Sub

0 Comments:

Post a Comment

<< Home