Dictionary Object in VBScript
Index -:
1. Dictionary Object
1. Dictionary Object
2. Disconnected recordset object
In Previous post,
we seen usage of array in VB script, but array is bind with index. Obviously arrays
provide a quick and easy way to track one-dimensional data, but when you have
multidimensional data, it can become difficult to keep track of the various index
based data. It can also become exceedingly complex to try to sort the data.
Because of that, if you do have multidimensional data you might want to
consider one of the following alternatives
VB Script
engine provides two alternative ways mentioned as below.
1. Dictionary object - : We can say Dictionaries are great
inventions, everything is in order and easy to use, can be access through string or keys.
A Dictionary object is a little bit like an array in that it can hold a
list of values. Those values can be of just about any type: strings, dates,
numbers, and so on. You can use a For each loop to loop through the objects in
a Dictionary object. However, while an array is a great way to iterate through
a list of values, its functionality is somewhat limited compared to the
Dictionary object.
Dictionary object consists of a collection of that are called key-item
pairs, each entry of collection consist of two elements keys and items.
A key is a unique entry: no two keys within a single Dictionary object
can be the same the item is just value that that goes along with a particular key, you an have as many duplicate Items as you want and its only the keys that need to be unique.
For example -:
Keys
|
Items
|
Oraganization1
|
Microsoft
|
Oraganization2
|
Google
|
Oraganization3
|
hpe
|
In above example keys are unique and it should be but Items can be
duplicate or vary.
More important thing is Dictionary object works in memory, just like an
array. You can create the Dictionary object within your script and use the
object for whatever you need to use it for then it disappears when the script
ends.
Usage of Dictionary Object -: Create dictionary object using CreateObject method
‘ Create dictionary object instance
Set objDictionary = CreateObject("Scripting.Dictionary")
|
Once dictionary object instance is created using two parameter first is
keys pair and second is items.
Syntax -: objDictionary.Add
“Keys” , “Items”
objDictionary.Add
“Oraganization1”, “Microsoft”
objDictionary.Add
“Oraganization2”, “Google”
objDictionary.Add
“Oraganization3”, “hpe”
|
Once all keys with values are ready you can setup for each loop to
iterate or for loop.
Now you can have dictionary object methods and properties access mentioned
below
Methods
|
Description
|
Add
|
Adds a new
item to the dictionary
|
Exists
|
Verifies
whether a given key exists in the dictionary
|
Items
|
Returns an
array with all the values in a dictionary
|
Keys
|
Returns an
array with all the keys in a dictionary
|
Remove
|
Removes the
item identified by the specified key
|
RemoveAll
|
Removes all
the items in the dictionary
|
Dictionary
object supported properties mentioned below
Properties
|
Description
|
Count
|
Returns number
of items in dictionary
|
Item
|
Sets and
returns an item for the specified key
|
Key
|
Change in
item key
|
Examples for
add and remove key -:
Set objDictionary = CreateObject("Scripting.Dictionary")
‘ Add keys in
dictionary object
objDictionary.Add
“FirstKey”,”FirstValue”
objDictionary.Add
“SecondKey”,”SecondValue”
‘ remove first keys
in dictionary object
objDictionary.Remove(“FirstKey”)
‘ it will remove
first key from dictionary object collection list
|
We can add
multiple unique keys and items and while access them use unique key, also you
can remove all or empty dictionary collection using RemoveAll method.
‘ remove all dictionary collections list
objDictionary.RemoveAll
|
You can check
existence of dictionary keys in collection using following method
‘ Check keys existence in collection
objDictionary.Exists(“SecondKey”)
|
You can take
a count of keys using following properties
‘Check keys existence in collection
objDictionary.count
‘ also you can iterate loop until last key
pair
Dim DictKeys
Dim DictItems
DictKeys = objDictionary.keys
DictItems =
objDictionary.Items
For
iDictionaryIterator = 0 to objDictionary.count -1
Msgbox DictKeys(iDictionaryIterator)
Msgbox DictItems(iDictionaryIterator)
Next
|
Alos you can view video tutorial for more details
2. Disconnected recordset object - : to be continue....
if you have any query please comment below
Great website, looks very professional. Keep up the good work!
ReplyDeleteQTP Training in Chennai
QTP Course in Chennai
QTP Training in Porur
QTP Training in T Nagar
LoadRunner Training in Chennai
hp loadrunner training
javascript training in chennai
core java training in chennai