Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / VBScript

VB6 Perl HASH Functionality

Rate me:
Please Sign up or sign in to vote.
3.00/5 (7 votes)
5 Dec 2003CPOL3 min read 56.1K   763   9  
Achieve Perl HASH functionality in the VB6 IDE and at Runtime
Sample Image - VB6_EnumHASH.jpg

Introduction

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.

VB.NET
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)), ",")
      Debug.Print table(0)
    Else
      'error: does not exist
    End If 
 GetHashElement = table(0)
 ReDim table(0) As String
 
End Function

Updates

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.

VB.NET
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.

History

  • 5th December, 2003: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Hubris is like armor. He is afraid to take on a Coder project for what it may do to him, both financially and to his health.

WEB Bowser is a hack that makes it easy to acquire internet content for archival. Its also possible to PROXY it, serving it to the internet as well. Given this capability, people will change the internet again.

I envision a programmable server that will acquire content for proxy. Users will then add proxy content to their own, making it available almost as if it were their own. We are already "information collectors." Its the opinions that are getting real hard to find.

-

Comments and Discussions

 
-- There are no messages in this forum --