Dictionary Object is
used when we want to save our data in the form of Key and Value Pair.
It has following
methods.
Add, Exist, Keys,
Items, Count, Remove, RemoveAll
Let’s see each of
them one by one with example.
--------------------------------------------------------------------------------------------
Add
Description: Add Method is used to add key and value in the dictionary
object.
Syntax: ObjDic.Add “Key”, “Items”
Return Type: None
Example: Dim ObjDic
ObjDic =
CreateObject(“Scripting.Dictionary”) ‘Create Dictionary Object
ObjDic.Add “Key1”,
“Item1”
----------------------------------------------------------------------------------------------
Count
Description: Count method is used to get the number of items in the
dictionary object.
Syntax: ObjDic.Count
Return Type: Long
Example: Dim ObjDic, Var_Count
ObjDic =
CreateObject(“Scripting.Dictionary”)
‘Create
Dictionary Object
ObjDic.Add “Key1”,
“Item1”
ObjDic.Add “Key2”,
“Item2”
Var_Count =
ObjDic.Count
MsgBox Var_Count ‘Display
2 as we added only two items in the dictionary object.
------------------------------------------------------------------------------------------------- Exists
Description: Exist method is used to check whether is given key is
present in the dictionary object or not.
Syntax: ObjDic.Exists(“Key”)
Return Type: A Boolean Value
Dim ObjDic, Var_Key
ObjDic =
CreateObject(“Scripting.Dictionary”)
‘Create
Dictionary Object
ObjDic.Add “Key1”,
“Item1”
ObjDic.Add “Key2”,
“Item2”
Var_Key =
ObjDic.Exists(“Key1”)
MsgBox Var_Key ‘Display True
Var_Key =
ObjDic.Exist(“Key5”)
MsgBox Var_Key ‘Display
False
-------------------------------------------------------------------------------------------
Item
Description: Item method is used to Set OR Get the value of the given
key.
Syntax: ObjDic.Item(“Key”)
Return Type: A variant value
Example: Dim ObjDic, Var_Val,
ObjDic =
CreateObject(“Scripting.Dictionary”) ‘Create
Dictionary Object
ObjDic.Add “Key1”,
“Item1”
ObjDic.Add “Key2”,
“Item2”
Var_Val =
ObjDic.Item(“Key1”)
MsgBox Var_Val ‘Display Item1
ObjDic.Item(“Key1”) =
“New_Item”
Var_Val =
ObjDic.Item(“Key1”)
MsgBox Var_Val ‘Display
New_Item
-----------------------------------------------------------------------------------------------
Items
Description: Items method is used to get an Array variable containing all
the items in the dictionary object.
Syntax: ObjDic.Items
Return Type: An Array variable
Example: Dim ObjDic, Var_Array
ObjDic =
CreateObject(“Scripting.Dictionary”)
‘Create
Dictionary Object
ObjDic.Add “Key1”,
“Item1”
ObjDic.Add “Key2”,
“Item2”
Var_Array =
ObjDic.Items
For each Val in
Var_Array ‘To
display each item of dictionary object.
MsgBox Val
Next
------------------------------------------------------------------------------------------------
Key
Description: Key method is used to change the key associated with the
item
Syntax: ObjDic.Key(“Key”)
Return Type: None
Example: Dim ObjDic, Var
ObjDic =
CreateObject(“Scripting.Dictionary”)
‘Create
Dictionary Object
ObjDic.Add “Key1”,
“Item1”
ObjDic.Add “Key2”,
“Item2”
ObjDic.Key(Key1) = “New_Key”
Var = ObjDic.Item(“New_Key”)
MsgBox Var
‘Display Item1
-------------------------------------------------------------------------------------------
Keys
Description: Keys method is used to get an Array variable containing all
the Keys in the dictionary object.
Syntax: ObjDic.Keys
Return Type: An Array variable
Example: Dim ObjDic, Key_Array
ObjDic =
CreateObject(“Scripting.Dictionary”)
‘Create
Dictionary Object
ObjDic.Add “Key1”,
“Item1”
ObjDic.Add “Key2”,
“Item2”
Key_Array = ObjDic.Keys
For each Var_Key in Key_Array ‘To
display each Key present in dictionary object.
MsgBox Var_Key
Next
------------------------------------------------------------------------------------------------
Remove
Description: Remove method is used to remove the specified Key from the
dictionary object.
Syntax: ObjDic.Remove(“Key”)
Return Type: None
Example: Dim ObjDic, Var
ObjDic =
CreateObject(“Scripting.Dictionary”)
‘Create
Dictionary Object
ObjDic.Add “Key1”,
“Item1”
ObjDic.Add “Key2”,
“Item2”
ObjDic.Remove(“Key1”)
If ObjDic.Exists(“Key1”)
Msgbox “Given Key is Present”
Else
Msgbox “Given Key is not Present”
End If
-----------------------------------------------------------------------------------------
RemoveAll
Description: RemoveAll method is used to remove all the information (Key
and Item) from the dictionary object.
Syntax: ObjDic.RemoveAll
Return Type: None
Example: Dim ObjDic,
ObjDic =
CreateObject(“Scripting.Dictionary”)
‘Create
Dictionary Object
ObjDic.Add “Key1”,
“Item1”
ObjDic.Add “Key2”,
“Item2”
ObjDic.RemoveAll
If ObjDic.Exists(“Key1”)
Msgbox “Given Key is Present”
Else
Msgbox “Given Key is not Present”
End If
Note: You will get
an error message “This Key is already associated with an element of this
collection”, if you try to associate same Key with two Items.
No comments:
Post a Comment