ASP custom hashtable class

Source: Internet
Author: User

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.

 
 
  1. <%
  2. Class hashtable
  3. '// Code by shaoyun
  4. '// Date: 11:46:14
  5. '// Site: devjs.com
  6. Private m_keys, m_vals, m_cnt, m_repeat
  7.  
  8. Private sub class_initialize ()
  9. M_keys = array ()
  10. M_vals = array ()
  11. M_cnt = 0
  12. M_repeat = false
  13. End sub
  14.  
  15. Private sub class_terminate ()
  16. End sub
  17.  
  18. 'Whether repeated key names are allowed
  19. Public property let repeat (boolval)
  20. M_repeat = boolval
  21. End Property
  22.  
  23. 'Get the total number of key-value pairs
  24. Public property get count ()
  25. Count = m_cnt
  26. End Property
  27.  
  28. 'Get the key value
  29. Public default property get keys (keyname)
  30. Dim I
  31. For I = 0 to m_cnt-1
  32. If keyname = m_keys (I) Then keys = m_vals (I)
  33. Next
  34. End Property
  35.  
  36. 'Add a key value
  37. Public sub add (Key, Val)
  38. Dim done
  39. Done = false
  40. If m_repeat then
  41. Done = true
  42. Else
  43. If not exist (key) then done = true
  44. End if
  45. If done then
  46. Redim preserve m_keys (m_cnt)
  47. Redim preserve m_vals (m_cnt)
  48. M_keys (m_cnt) = Key
  49. M_vals (m_cnt) = Val
  50. M_cnt = m_cnt + 1
  51. End if
  52. End sub
  53.  
  54. 'Check whether the key exists
  55. Public Function exist (keyname)
  56. Exist = false
  57. Dim I
  58. For I = 0 to m_cnt-1
  59. If keyname = m_keys (I) then
  60. Exist = true
  61. Exit
  62. End if
  63. Next
  64. End Function
  65.  
  66. 'Export data
  67. Public Function dump ()
  68. Dim I, ostr
  69. For I = 0 to m_cnt-1
  70. Ostr = ostr & "array (" "& m_keys (I) &" "," & m_vals (I )&"),"
  71. Next
  72. If m_cnt> 0 then ostr = mid (ostr, 1, Len (ostr)-1)
  73. Dump = "array (" & ostr &")"
  74. End Function
  75.  
  76. 'Assign values through Arrays
  77. 'Array value transfer Conventions: array (Array ("Item1", 1), array ("item2", 2 ))
  78. Public sub assignbyarray (ary)
  79. Dim I
  80. For I = 0 to ubound (ary)
  81. If isarray (ary (I) then
  82. Add ary (I) (0), Ary (I) (1)
  83. End if
  84. Next
  85. End sub
  86. End Class
  87.  
  88. 'Use instance
  89. Set ht = new hashtable
  90. Ht. Add "Haha", 2
  91. Ht. Add "Wawa", 4
  92. Ht. Add "Xixi", 3
  93. Ht. Add "Xixi", 6
  94. Ht. assignbyarray (Array ("A", 1), array ("B", 2 )))
  95. Response. Write Ht. Dump
  96. Response. Write Ht. Count
  97. Response. Write HT ("Haha ")
  98. Set ht = nothing
  99. %>

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.