View Single Post
 
Old 07-20-2017, 01:23 AM
slaycock slaycock is offline Windows 7 64bit Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

I'm just noticing that there are a number of items which use strings labels where the 'elegance' of the code might be improved by enumertion based on string rather than Longs

Such items include

• Custom Document Properties
• Custom XML Parts
• CustomUI Ribbon Control Identifiers
• Collections/Scripting Dictionaries
• User types that need string labels (e.g. user defined styles.

For example I have a set of names for elements in a CustomXML part. There are many sets of these elements.

Code:
Enum XMLTags

  TagName1 = "Name1"
  TagName2 = "Name2"
  TagName3 = "Name3"
....
  TagName25

End enum


For myTag = TagName1 to TagName13

  <Code for each tag>

Next
Rather than

defining the TagNames as Constants, Adding the Constant to an Array, defining an enumeration to index the TagNames in the array and then

Code:
Enumeration for tag index
Array for tag names
Assgning values to the name array
 
then

For myTagIndex = TagNameArray(enTagName1) to TagNameArray(enTageName13)

<code relating to tag>
Next
Or constructing the tag name on the run

Code:
Const  TagBase as string="Name"
Dim myTag as String
Dim myTagIndex as Long

For myTagIndex=1 to 13
  
  myTag = TagBase & myTabIndex
 if Dictionary(Key:=myTag).objectitem=<a value> then

end if

Next

None of the above cause me problems, but they do add an amount of coding 'cruft' that could be avoided with enumerated strings.

Now I hold my hands up and admit that I'm self taught with the aid of this forum, VBA in a Nutshell book, the sometimes useful MS help pages and other random reading via Google so it may be that I'm missing something very obvious.

In a nutshell I'm not stuck, just wondering about the best way to improve the readability of my code.
Reply With Quote