![]() |
|
|
|
#1
|
|||
|
|||
|
I know these can't be done in VBA but in my current project I have a number of cases where a string based enumeration would save a lot of sideways coding.
So this is really a question of how do you program around such a restriction. |
|
#2
|
||||
|
||||
|
What do you mean by 'string based enumeration' and 'sideways coding'? What are you trying to achieve?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
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 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 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. |
|
#4
|
||||
|
||||
|
You could just add the items to an array directly:
Code:
Sub Demo()
Dim i As Long, myArray()
myArray = Array("Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8")
For i = LBound(myArray) To UBound(myArray)
MsgBox myArray(i)
Next
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
|||
|
|||
|
The array approach was one of my first solutions (except I was using split with a single string). But to make the code more meaningful I also defined an enumeration to index the array.
This allowed me to say things like docXMLProp(<propertyname>) Which still leaves what should be a constant writable. So I made the array behave like a constant by putting it in a class. That leaves me with the choice of one get statement per enumeration value docXMLProp.<propertyname> or a single get property with a case statement to allow the form docXMLProp.item(<propertyname>) or docXMLProp(<propertyname>) if you go through the rigamarole of exposting the class, setting the default property as item then reimporting. |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
How to Dim a substring between two strings
|
dwirony | Word VBA | 4 | 10-28-2016 07:51 AM |
| VBA code to extract strings | twozedz | Excel Programming | 1 | 05-15-2016 06:00 AM |
Search with multiple strings
|
silverspr | Excel | 7 | 03-03-2013 04:45 PM |
Removing Character Strings
|
digitalhecht | Word | 2 | 10-17-2011 12:53 PM |
update style of all strings available between two specific strings
|
vikrantkale | Word | 1 | 03-28-2011 06:13 PM |