The below things can be done using Item Property:
- We can add new item to Dictionary Object(I have shown already)
- We can update a item in Dictionary Object(Same technique)
- We can read an Item from Dictionary Object
The Items strategy is utilized to retrieve the majority of the things in a specific Dictionary article and store them in an exhibit.
a = myVar.Items ' Get the items. For i = 0 To myVar.Count -1 ' Iterate the array. s = s & a(i) & " " ' Create return string. Next DicDemo = s
The Remove technique is utilized to evacuate a solitary key/thing pair from the predetermined Dictionary object.It is also important to use an Exist() method before use of remove() method. Otherwise it will throw an error.
The RemoveAll technique is utilized to expel all the key/thing sets from the predefined Dictionary object.
myVar.Remove("b") ' Remove second pair. a = myVar.RemoveAll ' Clear the dictionary.
Summary of Dictionary Object:
|Method or Property||Return||Description|
|Add||NA||Adds a new member to the dictionary|
|Exists||Boolean true false||test if a given key is in
|Items||array||Returns all the values in a dictionary in the form of an array|
|Keys||array||Returns all the keys in a dictionary in the form of an array|
|Remove||NA||Removes the item corresponding to the
|RemoveAll||NA||make the dictionary empty by removing all members|
|Count||int||Returns the number of items in a
|Item||Variant||Sets and returns an item for the
|Key||NA||Changes an item’s key|
Difference between Array and Dictionary Object in VBScript or UFT?
Imagine a table like below:
These kind of data structure is tough to read via Array. As we need to loop through the entire data structure using loop variables to get the correct data.One variable will go along with the columns and other one through rows.
Dictionary Objects on the other hand solve these kind of issues by using an Associative Array concept. It means each item in the rows has a unique key associated with it.
Both arrays and dictionaries are holders and can be perused sequentially (for example arrays can be listed by methods for a file and dictionaries by methods for a key). Yet, while arrays keep up the request among objects, dictionaries not.
What’s more, with dictionaries you have the likelihood to get to a particular element with a particular key in a more easy to understand way (a memory aide one).Alternatively we can use comments.
An Array hold an index value as numeric though in dictionary object, record esteem can be string. Thus while alluding any an incentive with key, a key can have any name related with an esteem.
While both can be used in our scripts,Dictionary objects in general are used as global variable and arrays we use as local variable.
- An array is a like holder object which holds a fixed number of estimations of a solitary kind.
- We can have dynamic array.
- Array does not have meany methods like Dictionary.
- Arrays can be dynamic in qtp/uft or some other programming ex: Redim to change the array estimate
- Before utilizing an array we should announce the extent of an array.
- If we need to expand the array measure in content we need to utilize Redim articulation to build the array.
- QTP/UFT does not have any technique to discharge the memory on the off chance that any component isn’t required.
- We can have multi dimensional array to work with.
- Deletion from inside of an Array costs more as other elements need to move up. For this we need to create one more array and transfer the remaining items.This back and forth operation comes with huge cost interms of speed.
Dictionary object has numerous builtin capacities which can include, expel, get to the components utilizing one of a kind key.
- QTP/UFT does not have any idea like dynamic Dictionary
- setting up size of the dictionary in QTP/UFT is not required.
- Not required to utilize any announcement to include additional element,just use include method.
- Dictionary supports many methods like add,delete etc.
- Can expel components which is never again valuable.
- We do not have multi dimensional Dictionary to work with.
- The deletion process is super easy. The internal mechanism of Dictionary automatically moves the elements up upon deletion of an item.
What’s the final Call?
Dictionary object was utilized with the record-index(key) being string. On account of array, the list can be ONLY numeric. At that point obviously we have some undeniable favorable circumstances like referencing an incentive with an important keys and so forth. While Array gives us flexibility to use duplicate entry, Dictionary object does not allow duplicate entry.
Dictionary Object is more useful and versatile replacement of an Array.