4

I've just discovered VBScript's GetRef function, which gets a reference to the function named by its argument. Is there any way to get a reference to a method in this way? I have a hunch that VBScript doesn't offer the sophistication of binding needed to do so, but it would sure be nice.

Thom Smith
  • 13,916
  • 6
  • 45
  • 91

2 Answers2

2

No, GetRef doesn't support class methods.

Helen
  • 87,344
  • 17
  • 243
  • 314
1

There is a workaround for this, see my answer here

Here the full sample

Const forReading = 1, forWriting = 2, forAppending = 8, CreateFile = True
Set my_obj = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\temp\test.txt", forWriting, CreateFile)

Function my_function(my_obj, method, text)
  command = "my_obj." & method & " """ & text & """"
  ExecuteGlobal command
End Function

'make a reference to our function
Set proc = GetRef("my_function") 
'and call it with parameters, the first being the method invoked
Call proc(my_obj, "WriteLine", "testing")

'cleanup'
my_obj.Close
Set my_obj = Nothing
peter
  • 41,770
  • 5
  • 64
  • 108