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.")
%>
Comments (7) Trackbacks (0)
  1. Hi,

    Thanks for the great article. I was needed the same thing that you provided.

    But I have a one question/problem. When I tries to store the above object in a session and when I get it anywhere else, I am not able to access its values or any of the above method. Can you help me in this issue.

    I am really in trouble and hurry. I will be grateful to you, if you can help.

    Thanks in advance.

    Regards.

  2. You cant store this object in a Session, you would have to create a new object on the next page and re-populate the array.

  3. Hi Greg,

    Thanks for reply.

    But how can I pass data of this object to next page and repopulate it without using Session. Please help me.

    Waiting for your reply.

  4. Greg “Long life to Classic ASP”, here a very small contribution to update an existent value

    Public Function SetItem(byVal Key, byVal Value, byVal NewValue)
    On Error Resume Next
    If Dict.Item(Key).Exists(Value) Then
    Dict.Item(Key).Item(Value) = NewValue
    End If
    On Error Goto 0
    End Function

  5. Thanks for the contribution Mack, much appreciated. and to Kapil if you want to pass the values to another page you are gonna have to save the values somewhere, Session would be the best in my opinion if you don’t want to yet save it to a database, second I would say save it to the database and then repopulate the object on the next page. you could also pass the data in form fields or query strings but that opens it up for injection attacks, and i would not recommend it unless it is being used in a secured environment.

    anyway you do it you are gonna have to take the data and repopulate the object, I cant think of anyway of maintaining the object state across multiple pages.

  6. Hi

    This looks perfect for what I want to do but it is behaving very strange for me. Whenever is use Exists it always returns true, even on a completely new MultiDimensionalDictionary which should be empty. On top of that the very process of checking to see if a record exists, actually creates the record. So if I do lots of exists tests they all return true and then if I iterate the records they are there. I’m I just getting something fundamentally wrong?

    thanks
    Chris

  7. Hi Chris, Try wrapping the Exists call like Cbool(Dict.Exists(“test”, “1″)) and see if it returns true or false. I just tried a test and I was able to get a false from requesting a non existent value


Leave a comment

(required)


*

No trackbacks yet.