Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Who can tell me about "Scripting.Dictionary"? 2

Status
Not open for further replies.

Ovatvvon

Programmer
Feb 1, 2001
1,514
US
Hey, I'm pickin through some code, and couldn't find anything on this that was real obvious on the web...

Does anyone know that the scripting.dictionary is, and what one would use it for?

I came accross it in somthing like this:
Server.CreateObject("Scripting.Dictionary")

Thanks in advance,
-Ovatvvon :-Q
 
You can use the dictionary object to store information in the object based on key and description sort of like how a real dictionary works. It basically just stores information for you that you can return by just using the key.

For example

<%

Dim objDictionary, strDescription

'create the dictionary object
set objDictionary = CreateObject(&quot;Scripting.Dictionary&quot;)

'add keys and descriptions to the object
'param 1 is the key and param 2 is the description
'related to the key

objDictionary.add &quot;Key1&quot;, &quot;Description1&quot;
objDictionary.add &quot;Key2&quot;, &quot;Description2&quot;

'retrieve the information back into a variable
'you can use the item method and pass it the key for
'the description you want to return

strDescription = objDictionary.item (&quot;Key1&quot;)

Response.Write &quot;Key1 = &quot; & strDescription

strDescription = objDictionary.item (&quot;Key2&quot;)

Response.Write &quot;Key2 = &quot; & strDescription

%>

You can also change the descriptions the same way as retrieving just put it on the left side.

objDictionary.item(&quot;Key1&quot;) = &quot;NewDescription1&quot;

You can change the key by using the key method.

objDictionary.key(&quot;Key1&quot;) = &quot;NewKey1&quot;

And you can also loop through the object to return all the keys and descriptions like with other collections.

For Each key in objDictionary
...code here
Next

Hope this helps. I have never actually found a time when I felt like using it but who knows what tomorrow will bring :)

 
Perfect. Thank you very much. (It was used for a collection of days in a month for a calendar on any givin month, on any given year.

Star for you! :)
-Ovatvvon :-Q
 
Four things.
If you do not set .CompareMode = 1 (vbTextCompare) before adding any items, Key names are case sensitive. That is important beacuse of the following.

If you misspell a Key, a new empty item is added with that key
e.g. a = objDict.Item(&quot;tabel&quot; ' get TABLE item
create a &quot;tabel&quot; item instead of returning a &quot;table&quot; item.

You need not use the .Add method. A new Key name appearing on the right sude of aassigment will create a new item

Do not leave off default properties when storing the contemts of objects into the dictionary, otherwise a reference to the object will be stored instead of the data. If that object happens to be a form control, you can find forms being implicitly loaded after they have been unloaded, when the dictionary item is referenced
Use
objDict(&quot;textdata&quot;) = textbox1.text


Documentation
&quot;Remarks

If key is not found when changing an item, a new key is created with the specified newitem. If key is not found when attempting to return an existing item, a new key is created and its corresponding item is left empty.&quot;

I use Const to define distionary keys sp that misspelling causes a Compile Error.

Const dkTable = &quot;table&quot;

objDict(dkTable) = &quot;Kitchen&quot;

 
ok, I tried to use this to suppliment server side code. Can it only work through client side?

for instance, I have a calendar using vb classes. in the manipulation of days in the month, etc...I can set events for each day specifically for that day, or for that day of that month in every year. (Every July 4 is USA's B-DAY, etc).

MyCalendar.Days(4).AddActivity &quot;<small><small>New Years</small></small>&quot;, &quot;limegreen&quot;

Where MyCalendar is the new class in the main page, days(4) is using the days sub function to specify day 4, and AddActivity as part of the associtation with each calendar day function. The next part is the next to appear on the day on the calendar, and the last part is the background color of that event/message.

I wanted

Set obj = Server.CreateObject(&quot;Scripting.Dictionary&quot;)
obj.add &quot;msgOne&quot;, &quot;&quot;&quot;MyCalendar.Days(4).AddActivity &quot;&quot;<small><small>New Years</small></small>&quot;&quot;, &quot;&quot;limegreen&quot;&quot;&quot;&quot;


And then where I'd normail insert the code, instead insert the dictionary item...


obj.item(&quot;msgOne&quot;)

But that won't put the item in place of that...so the page won't interact with it.

Any ideas?

-Ovatvvon :-Q
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top