It's Not A Bug, It's A Feature Just another Developer weblog

6May/107

Multi Dimensional Dictionary Object in Classic ASP

Well, the name really says it all, there is no real built in object in ASP for doing anything like this so I threw this together as a little experiment. feel free to use and abuse it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<%
'Option Explict

Class MultiDimensionalDictionary
'######################################################################
'Named Dictionary Recordset Object
'######################################################################
Public SetKey
Private Dict
Private AddNewRecord
'######################################################################
Private Sub Class_Initialize
Set Dict = Server.CreateObject("Scripting.Dictionary")
Set AddNewRecord = Server.CreateObject("Scripting.Dictionary")
End Sub
'######################################################################
Private Sub Class_Terminate
Set Dict = Nothing
Set AddNewRecord = Nothing
End Sub
'######################################################################
Public Sub Update
Dict.Add SetKey, AddNewRecord
Set AddNewRecord = Server.CreateObject("Scripting.Dictionary")
End Sub
'######################################################################
Public Function GetCollection
Set GetCollection = Dict
End Function
'######################################################################
Public Sub SetField(byVal Key, byVal Value)
If AddNewRecord.Exists(Key) = False Then
AddNewRecord.Add Key, Value
Else
AddNewRecord(Key) = Value
End If
End Sub
'######################################################################
Public Function Records
Records = Dict.Keys
End Function
'######################################################################
Public Function Fields(byVal PrimaryKey)
Fields = Dict(PrimaryKey).Keys
End Function
'######################################################################
Public Function Item(byVal Key, byVal Value)
On Error Resume Next
If Dict.Item(Key).Exists(Value) Then
Item = Dict.Item(Key).Item(Value)
End If
On Error Goto 0
End Function
'######################################################################
Public Function Exists(byVal Key, byVal Value)
'On Error Resume Next
If IsNull(Value) Or Value = "" Then
Exists = Dict.Item(Key).Exists
Else
Exists = Dict.Item(Key).Exists(Value)
End If
'On Error Goto 0
End Function

Public Function Count
Count = Dict.Count
End Function
'######################################################################
End class
'######################################################################
'Example Usage
Dim Dict    'The Dict Object
Dim Record    'The Record Object
Dim Field    'The Field Object
Set Dict = New MultiDimensionalDictionary 'Create an Instance of the Class
'######################################################################
'ADDING RECORDS TO DICTIONARY
'######################################################################
'EACH NEW KEY MUST BE UNIQUE OTHERWISE IT WILL OVERWRITE THE PREVIOUS
'KEY WITH THE SAME NAME.
'EACH FIELD MUST ALSO BE UNIQUE IN EACH RECORD OTHERWISE IT WILL
'OVERWRITE THE PREVIOUS KEY IN THE SAME RECORD WITH THE NEW VALUE

Response.Write "Dict.Count = " &  Dict.Count &  "<br/>"

Dict.SetKey = "First" 'Key the first record
Dict.SetField "1", "Record1 Field 1 Value"
Dict.SetField "2", "Record1 Field 2 Value"
Dict.SetField "3", "Record1 Field 3 Value"
Dict.SetField "4", "Record1 Field 4 Value"
Dict.SetField "5", "Record1 Field 5 Value"
Dict.Update 'Bind the new record and preapre for the next record

Dict.SetKey = "Second" 'Key the second record
Dict.SetField "Alpha", "Record2 Field 1 Value"
Dict.SetField "Beta", "Record2 Field 2 Value"
Dict.SetField "Charlie", "Record2 Field 3 Value"
Dict.SetField "Delta", "Record2 Field 4 Value"
Dict.SetField "Echo", "Record2 Field 5 Value"
Dict.Update 'Bind the new record and preapre for the next record

Dict.SetKey = "Third" 'Key the third record
Dict.SetField "A", "Record3 Field 1 Value"
Dict.SetField "B", "Record3 Field 2 Value"
Dict.SetField "C", "Record3 Field 3 Value"
Dict.SetField "D", "Record3 Field 4 Value"
Dict.SetField "E", "Record3 Field 5 Value"
Dict.Update

Dict.SetKey = 4 'Key the third record
Dict.SetField 0, "Record4 Field 1 Value"
Dict.SetField 1, "Record4 Field 2 Value"
Dict.SetField 2, "Record4 Field 3 Value"
Dict.SetField 3, "Record4 Field 4 Value"
Dict.SetField 4, "Record4 Field 5 Value"
Dict.Update

Response.Write "Dict.Count = " &  Dict.Count &  "<br/>"
'######################################################################
'ENUMERATE RECORDS
'This method shows you how to enumerate the record/field collection.
'######################################################################
Response.Write("-- ENUMERATE RECORDS --<br>")
For Each Record in Dict.Records
Response.Write("<strong>Record: " &  Record &  "</strong><br>")
For Each Field in Dict.Fields(Record)
Response.Write("& _nbsp;& _nbsp;& _nbsp;<strong>Field Name:</strong> " & Field &  " : <strong> Value:</strong> " &  Dict.Item(Record, Field) &  "<br>")
Next
Response.Write("<hr>")
Next
'######################################################################
'CALL A SINGLE NAMED RECORD WITH THE ITEM METHOD
'this example show you how to call a single record/field value. if record does not
'exist the call will return an empty string
'######################################################################
Response.Write("-- CALL A SINGLE NAMED RECORD --<br>")
Response.Write( "<strong>Dict.Item(""First"", ""1"") = </strong>" & Dict.Item("First", "1") &  "<br>")
Response.Write( "<strong>Dict.Item(""Second"", ""Alpha"") = </strong>" & Dict.Item("Second", "Alpha") &  "<br>")
Response.Write( "<strong>Dict.Item(""Third"", ""c"") = </strong>" & Dict.Item("Third", "C") & "<br>")
Response.Write( "<strong>Dict.Item(4, 1) = </strong>" & Dict.Item(4, 1) &  "<br>")
Response.Write("<hr>")
'######################################################################
'USING THE EXISTS METHOD
Response.Write("-- EXISTS --<br>")
Response.Write( "<strong>Dict.Exists(""First"", ""1"") = </strong>" & Dict.Exists("First", "1") & "<br>")
Response.Write("<hr>")
'######################################################################
'TODO: Need to add a method for updating a existing record.
'TODO: Add a method for adding new records to an existing set.
'######################################################################
Set Dict = Nothing
'######################################################################
If err.number <> 0 Then
Response.Write err.number & " : " & Err.Description
End If
Dim response_time
response_time = cdbl(timer() - s_time)
Response.Write("This page was generated in " & response_time & " seconds.")
%>