Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GetRef - Interesting question
#1
Not Solved
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."
Reply
#2
Not Solved
(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
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)