I have found a helpful way to use enumerations that can be referenced in the IDE, while coding logic. The enumerations are also useful in returning hash values. The simple method I use makes it possible to imitate Perl hash functionality in VB with the added bonus that all Public enumerations are referenced for the object in the IDE.
I needed a way to return strings for a HTTP protocol wrapper. There are so many things to keep straight. When I found this way to use enumerations to access HASH strings, I knew it was the right approach.
Using the Code
Access an enumerated HASH value. (Put an enumerating value in the box and click on > button.) External method passes the enumerated hash and index in as a string.
Private Function GetHashElement(hash As String, hash_enum As String) As String
Dim loc As Long Dim table() As String
loc = InStr(1, hash, hash_enum, vbTextCompare)
If loc > 0 Then
table = Split(Right(hash, Len(hash) - (loc - 1)), ",")
GetHashElement = table(0)
ReDim table(0) As String
Will be made in the form of usage in actual applications.
Points of Interest
Use of enumerated subtypes will achieve improved readibility in code that makes extensive references to different enumerated values.
With use of Enumerations for the HASH index, it's possible to access the enumerated strings (in this demonstration, they are the reasons) so that there is no need to fumble around hardcoding errors from a reference book. As well, the reason phrases are accessed at runtime using the enumerations to get them from the HASH. Use is intended for a class object that wraps the enumerations and methods. This is only a demonstration that does not emphasize enough the advantage of having the enumerated table available when coding external logic and references.
To get and test the IDE enumerations, create a new class object using the HASH and the enumeration. Put the GetHash method into the object as a private one and make a public method to use it. Save the class object, then add it to your project. Delete any duplicate enumerations in form_1 and declare a variable AS the enumerated subtype. After this, any references in the IDE for hardcoding this variable (as when building methods) will display the enumerated references by their names, not their enumerations. Calls to the public method can be made passing the value as the enumerated subtype where it can be converted to a string and used to index the HASH in the GetHash method. To improve readibility, any values hardcoded as enumerated types are given the actual enumerated string during development of your source code. The enumerated string values are in your source listing.
Dim code as status_code
code = (at this point under the cursor is displayed the enumerated list to select) Select 'OK' if you desire to return an OK response. Select 'Not Found' to return the 404 error status. You see, this method makes it practical to code HTTP server responses without losing your mind.
I thought about using another INSTR search for the second separator, but using the SPLIT function here is probably as efficient. With very long hashes, this would change. You could expand on this method to suit your needs. For example, you might want to make it possible to have commas in the fields someday by using a different separator. Also, you might add a comma at the beginning of the HASH string to create a null field before the split. Then use this field to add an entry in the separated table.
Thank you for reading my article.
- 5th December, 2003: Initial post