I've been using the 'dot' operator for some "classes" I'm using, but I'm hitting a road block when trying to build a dynamic dispatch table.
To give an example of what I need to accomplish consider the following function. Effectively, it returns the attribute specified if the 'm' variable is assigned as expected when the function return attr is called using the '.' operator or a string stating that the attribute does not exist:
Function returnattr(attrname as String) as Object
if not m.DoesExist(attrname) then
return "Attribute " + attrname + " does not exist"
else
return m.Lookup(attrname)
end if
End Function
Now consider the following scenarios:
Sub test_dynamic_dispatch(t as object)
obj = CreateObject("roAssociativeArray")
obj["getattr"] = returnattr
obj["a"] = "a"
'Using dot operator to perform call
t.assertEqual("a", obj.getattr("a"))
'Using dot operater in an eval()
result = "NO VALUE ASSIGNED"
attrname = "a"
code_snippet = "result = obj.getattr(attrname)"
eval(code_snippet)
t.assertEqual("a", result)
'Using dot operator to do method dispatch
method = obj.getattr
t.assertEqual("a", method("a"))
End Sub
The first (explicitly using the dot operator) and second (using eval) approaches work as expected. In the case of the third (looking up the method then calling it) the 'm' variable is assigned to an empty roAssociativeArray. Is to be expected that the only time 'm' will be set is when the method is explicitly called with the dot operator even if the method is accessed with the dot operator initially?
-Mark