Micro Focus QTP (UFT) Forums
GetRef - Interesting question - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: GetRef - Interesting question (/Thread-GetRef-Interesting-question)



GetRef - Interesting question - vIns - 08-20-2012

Hi All,
Please see the program.

The requirement is to Capitelize the first letter of each word in a sentence.
The programmer has used regular expression and getref - to connect to a function which capitelizes the first letter of a given word.
This programs works as expected.

My only question is - where do we pass the arguments to the function 'Capitelize'?? It expects 3 arguments.


Code:
Dim replacement
Dim regEx : Set regEx = New RegExp ' Create a regular expression.
Dim myString : myString = "Cake, and grief COUNSELING, will be Available at the CoNcLuSiOn of the test."

regEx.Pattern = "\b\w+\b" ' \b = inside word boundary, \w = word character,
                          ' so this pattern matches each single word
regEx.Global = True       ' Set the scope to Global

Set replacement = getRef("Capitelize")

Function Capitelize(singleMatch, position, fullString)
    Capitelize = ucase(left(singleMatch,1)) & lCase(mid(singleMatch,2))
End Function

MsgBox regEx.Replace(myString, replacement)
' This results in: "Cake, And Grief Counseling, Will Be Available At The Conclusion Of The Test."



RE: GetRef - Interesting question - Hailesh - 09-21-2019

(08-20-2012, 06:59 AM)vIns Wrote: Hi All,
Please see the program.

The requirement is to Capitelize the first letter of each word in a sentence.
The programmer has used regular expression and getref - to connect to a function which capitelizes the first letter of a given word.
This programs works as expected.

My only question is - where do we pass the arguments to the function 'Capitelize'?? It expects 3 arguments.


Code:
Dim replacement
Dim regEx : Set regEx = New RegExp ' Create a regular expression.
Dim myString : myString = "Cake, and grief COUNSELING, will be Available at the CoNcLuSiOn of the test."

regEx.Pattern = "\b\w+\b" ' \b = inside word boundary, \w = word character,
                         ' so this pattern matches each single word
regEx.Global = True       ' Set the scope to Global

Set replacement = getRef("Capitelize")

Function Capitelize(singleMatch, position, fullString)
   Capitelize = ucase(left(singleMatch,1)) & lCase(mid(singleMatch,2))
End Function

MsgBox regEx.Replace(myString, replacement)
' This results in: "Cake, And Grief Counseling, Will Be Available At The Conclusion Of The Test."
Hi,

You must have got an error on line 'Set replacement = getRef("Capitelize")', here your function does not return
any value. Also if a function as per shared example returns any value, you need to collect it in a variable.

One can use getRef as per below example:

Function additionFunc(a, b)
additionFunc = a + b
End Function
x=5
y=6
Dim returnVal : returnVal = getRef("additionFunc")(x,y)
print "Result is: "&returnVal