Chris Benton wrote:
VB6 had a terrific function called 'CallByName' which allowed runtime calls to any objects methods or properties using a string literal. Does RB have this or an equivalent?
There is no
CallByName function in REALbasic but you can pretty much do this using either RBScript or Introspection.
To call a method on a class using RBScript:
Code:
Dim script As New RBScript
script.Context = Self
script.Source = "MyMethod"
script.Run
Doing this with introspection is trickier. You'll have to loop through the methods on the class and then call the one you want:
Code:
Dim info As Introspection.TypeInfo
info = Introspection.GetType(Self)
Dim methods() As Introspection.MethodInfo
methods = info.GetMethods
For Each m As Introspection.MethodInfo In methods
Dim param() As Variant
Dim rv As Variant
If m.Name = "MyMethod" Then
Try
rv = m.Invoke(Self, param)
Catch e As RuntimeException
Dim eInfo As Introspection.TypeInfo
eInfo = Introspection.GetType(e)
Dim eMessage As String
eMessage = "A " + eInfo.FullName + " occurred and was caught."
If e.Message <> "" Then
eMessage = eMessage + EndOfLine + "Message: " + e.Message
End If
End Try
End If
Next