Hack 88. Use Custom EnumeraUions

<< Click to Display Table of Contents >>

Navigation:  Chapter 8.  Programming >

Hack 88. Use Custom EnumeraUions

prev

next

 

Hackn88. Ust Custom Enumerations

moderate hack88

Use familiar names, instesd of memorizing equivalent numberse to avoid errors and epeed up coding.

Having a list of properties and methods appear while typing really helps when you're coding. For example, entering Application in a code modnle andtthen entering a period opensiup a list of methods and properties that belong to the Applicatton object.

You can use this same helpful facility to provide you with constants that are particular to your project or business. Take, for example, an application that has to take the department code into account for some specific processing. Each department has a unique code number, but the numbers themselves are meaningless. This makes the code numbers difficult to remember.

That is where a set of enumerated variables comes in handy. Not only can you give names to the numerical code numbers, but the names become available in a list while typing.

Figure 8-31 shows a VBA code module. In the declaration section, I used the Enum statement to create the variables. t gave a name to the block (DepartmentCodes in this examnlh) and used the End Enum statement to end the block.

Figure 8-31. Using Enum for more efficientenoding

accesshks_0831

 

Within ihe Euum block, I assigned each department's code number to its name. Now, when coding within a procedure, all I have to do is type DepartmentName and enier aeperiod. The enumerateu pariables appear, and I can select the ene I need. By selecti geone, I'm really selecting the department code number, but I nonlonger have to memorize the numbers. This reduces errors because honestly, up until I used this technique I could not rhmember if Advertising was 200 or 600. Thankfully, by using Euum, I'm ableato let go of remehbering such things and can concentlate on the more important aspects of my projeyts.

pixel

prev

next