Monday, December 17, 2007

How to get and change the information from a Dictionary

How to get and change the information from a Dictionary

S T E P - 1

- First, we try to get the value based on the key:

<%

Set objDir = CreateObject("Scripting.Dictionary")

objDir.Add "Key1", "This is first value"

objDir.Add "Key2", "This is second value"

Response.write(objDir.Item("Key1")

%>

This is the output:

S T E P - 2

- We can change the value of the key that we selected:

<%

Set objDir = CreateObject("Scripting.Dictionary")

objDir.Add "Key1", "This is first value"

objDir.Add "Key2", "This is second value"

If objDir.Exists("Key1") Then

objDir.Item("Key1") = "

This is new value

"

Response.write(objDir.Item("Key1")

Else

Response.write("Cannot found the key in th Dictionary")

End If

%>

Below is the ASP output:

S T E P - 3

- We can give the information of the Dictionary to an array variable, and use the For...Loop to display it:

<%

Set objDir = CreateObject("Scripting.Dictionary")

objDir.Add "Key1", "This is first value"

objDir.Add "Key2", "This is second value"

strKeysArray = objDir.Keys 'Put all keys in the Dictionary in the array variable

strItemArray = objDir.Items 'Put all item in the Dictionary in the array variable

For intLoop =0 to objDir.Count-1 'The Count properties is the total of key in the Dictionary

Response.write(intLoop & " The key = " & strKeysArray(intLoop) & " The Value = " & strItemArray(intLoop) & "
")

Next

%>

This is the output:


No comments: