QTP Math functions - Convert Set to Typed Array

Categories: MathSource code

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

Convert set to typed array

Public Function Set2TypedArray(ByVal stSet, ByVal sSetSeparator, ByVal sFormat)
 Dim Iter, dvArray
 If sSetSeparator = "" Then
 sSetSeparator = chrSetSeparator
 End If
 stSet = Replace(stSet, "\,", Chr(176))
 stSet = Replace(stSet, "\ ", Chr(187))
 dvArray = Split(stSet, sSetSeparator)
 For Iter=0 To UBound(dvArray)
 Select Case UCase(sFormat)
 Case "NUMBER"
 dvArray(Iter) = DblVal(Replace(Replace(Trim(dvArray(Iter)), Chr(176), ""), Chr(187), ""))
 Case "DATE"
 dvArray(Iter) = DateVal(Replace(Replace(Trim(dvArray(Iter)), Chr(176), ""), Chr(187), ""))
 Case "STRING"
 dvArray(Iter) = CStr(Replace(Replace(Trim(dvArray(Iter)), Chr(176), ","), Chr(187), " "))
 Case Else
 dvArray(Iter) = Replace(Replace(Trim(dvArray(Iter)), Chr(176), ","), Chr(187), " ")
 End Select
 Next
 Set2TypedArray = dvArray
End Function

Unit Tests

 dvArray = Set2TypedArray("1.0,-2, 3 5", ",", "Number")
 If dvArray(0) <> 1 Then
 Log.Error("Set2TypedArray failed")
 End If
 If dvArray(2) <> 0 Then
 Log.Error("Set2TypedArray failed")
 End If
 dvArray = Set2TypedArray("1.0,-2, 3 5", ",", "String")
 If dvArray(0) <> "1.0" Then
 Log.Error("Set2TypedArray failed")
 End If
 If dvArray(2) <> "3 5" Then
 Log.Error("Set2TypedArray failed")
 End If
 dvArray = Set2TypedArray("1.0,-2\,, \ 3 5", ",", "String")
 If dvArray(0) <> "1.0" Then
 Log.Error("Set2TypedArray failed")
 End If
 If dvArray(2) <> " 3 5" Then
 Log.Error("Set2TypedArray failed")
 End If
 dvArray = Set2TypedArray("1.0,-2\,, \ 3 5", ",", "Number")
 If dvArray(0) <> 1 Then
 Log.Error("Set2TypedArray failed")
 End If
 If dvArray(1) <> -2 Then
 Log.Error("Set2TypedArray failed")
 End If
 dvArray = Set2TypedArray("1.0,-2\,a, \ 3 5 , abc, 12/10/2001", ",", "")
 If dvArray(1) <> "-2,a" Then
 Log.Error("Set2TypedArray failed")
 End If

  • Leave a Reply

    * Required
    ** Your Email is never shared

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