There are dictionary objects in ASP, such as key-value pairs in hash tables. Such key-value pairs can be used in many places. Unfortunately, dictionary has many shortcomings, when I found that I could not use it flexibly, I thought it would be better to use arrays to customize a hash structure. I remember I wrote an arraylist ASP encapsulation before, so we have hashtable in this article.
- <%
- Class hashtable
- '// Code by shaoyun
- '// Date: 11:46:14
- '// Site: devjs.com
- Private m_keys, m_vals, m_cnt, m_repeat
-
- Private sub class_initialize ()
- M_keys = array ()
- M_vals = array ()
- M_cnt = 0
- M_repeat = false
- End sub
-
- Private sub class_terminate ()
- End sub
-
- 'Whether repeated key names are allowed
- Public property let repeat (boolval)
- M_repeat = boolval
- End Property
-
- 'Get the total number of key-value pairs
- Public property get count ()
- Count = m_cnt
- End Property
-
- 'Get the key value
- Public default property get keys (keyname)
- Dim I
- For I = 0 to m_cnt-1
- If keyname = m_keys (I) Then keys = m_vals (I)
- Next
- End Property
-
- 'Add a key value
- Public sub add (Key, Val)
- Dim done
- Done = false
- If m_repeat then
- Done = true
- Else
- If not exist (key) then done = true
- End if
- If done then
- Redim preserve m_keys (m_cnt)
- Redim preserve m_vals (m_cnt)
- M_keys (m_cnt) = Key
- M_vals (m_cnt) = Val
- M_cnt = m_cnt + 1
- End if
- End sub
-
- 'Check whether the key exists
- Public Function exist (keyname)
- Exist = false
- Dim I
- For I = 0 to m_cnt-1
- If keyname = m_keys (I) then
- Exist = true
- Exit
- End if
- Next
- End Function
-
- 'Export data
- Public Function dump ()
- Dim I, ostr
- For I = 0 to m_cnt-1
- Ostr = ostr & "array (" "& m_keys (I) &" "," & m_vals (I )&"),"
- Next
- If m_cnt> 0 then ostr = mid (ostr, 1, Len (ostr)-1)
- Dump = "array (" & ostr &")"
- End Function
-
- 'Assign values through Arrays
- 'Array value transfer Conventions: array (Array ("Item1", 1), array ("item2", 2 ))
- Public sub assignbyarray (ary)
- Dim I
- For I = 0 to ubound (ary)
- If isarray (ary (I) then
- Add ary (I) (0), Ary (I) (1)
- End if
- Next
- End sub
- End Class
-
- 'Use instance
- Set ht = new hashtable
- Ht. Add "Haha", 2
- Ht. Add "Wawa", 4
- Ht. Add "Xixi", 3
- Ht. Add "Xixi", 6
- Ht. assignbyarray (Array ("A", 1), array ("B", 2 )))
- Response. Write Ht. Dump
- Response. Write Ht. Count
- Response. Write HT ("Haha ")
- Set ht = nothing
- %>
Attaching the previously written arraylist link to the back is convenient and easy to use.
ASP custom arraylist class
Link: http://www.devjs.com/Article/25.aspx