preload

QTP Math functions – Check Item in Array

Posted by Albert Gareev on Feb 17, 2008 | Categories: MathSource code

Parent page: Service Functions – Math (QTP, VBScript)

Check whether an item is in array


Public Function isItemInArray(ByVal dvArray, ByVal sItem)
  Dim Iter
 
  If sItem = "" Then
    isItemInArray = False
    Exit Function
  End If
 
  If Not isArray(dvArray) Then
    isItemInArray = False
    Exit Function
  End If
 
  For Iter=0 To UBound(dvArray)
    If dvArray(Iter)= sItem Then
      isItemInArray = True
      Exit Function
    End If
  Next

  isItemInArray = False
End Function

Unit Tests


  boolRC = isItemInArray(Array(-1, 15, 15.0, 15.51, "a"), 15.5)
  If boolRC Then
    Log.Error("isItemInArray failed")
  End If
  boolRC = isItemInArray(Array(-1, 15, 15.0, 15.50, "a"), 15.5)
  If Not boolRC Then
    Log.Error("isItemInArray failed")
  End If
  boolRC = isItemInArray(Array("a", "abc", "dex", 10, "z"), "10")
  If boolRC Then
    Log.Error("isItemInArray failed")
  End If
  boolRC = isItemInArray(Array("a", "abc", "dex", 10, "z"), "z")
  If Not boolRC Then
    Log.Error("isItemInArray failed")
  End If
  boolRC = isItemInArray(Array(""), "")
  If boolRC Then
    Log.Error("isItemInArray failed")
  End If
  boolRC = isItemInArray(10, 10)
  If boolRC Then
    Log.Error("isItemInArray failed")
  End If


Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.