Wednesday, August 3, 2011

QTP Compare Two Dictionary Objects

The following function compares two Dictionary objects. This function assumes that the two dictionary objects hold simple values for example String, Date, Integer etc.

The code goes here:

###########################################################

Function CompareDictionary(oDict1, oDict2)

Dim i, arrKeys

'Check the most basic criteria - do the two Dictionary objects have the same number of items?

If oDict1.Count <> oDict2.Count Then
CompareDictionary = False
Exit Function
End If

'Compare all keys and their values

arrKeys = oDict1.Keys
For i = 0 to uBound(arrKeys)
'Compare keys
If Not oDict2.Exists(arrKeys(i)) Then
'oDict1 has a key which oDict2 doesn't have
CompareDictionary = False
Exit Function

End If

'Compare values
If oDict1(arrKeys(i)) <> oDict2(arrKeys(i)) Then
'oDict1 value for arrKeys(i) differs from oDic2
CompareDictionary = False
Exit Function
End If

Next

'No need to check if oDict2 has values and keys that oDic1 doesn't have,
'since oDict2 and oDict1 have the same number of values.
'So if oDict2 has every value & key oDict1 has, it can't have any extra values and keys

CompareDictionary = True 'If this executes, both Dictionaries match

End Function

###########################################################

------------------------------------------------------------------------------------------------------------------------------------
Have questions on this article? Feel free to ask me in the comments section below.

If you like this post, I would suggest you to subscribe my RSS feed to have future articles delivered to your email address directly.
----------------------------------------------------------------------------------------------------------------------------------

0 comments: