Operator_Lookup and ParamArray are two language features that have been in Xojo for a while now, but most probably don’t know why they were originally added. The need grew out from having to support COM on Windows.
First, let’s take a look at COM and its IDispatch interface. This interface is used by automation clients (like Xojo and VB) to access the COM component. At the high level view of this interface it is much like Introspection. The interface has methods to obtain a list of properties/methods that the COM component exposes and a way to Invoke them. For example, this is how one could use the ADO COM component to open a database connection using VB:
Dim Cnxn As Object Set Cnxn = CreateObject("ADODB.Connection") ' Open connection Cnxn4.Open("Pubs", "MyUserId", "MyPassword")
Under the hood, the Cnx4.Open call is communicating with the ADO component via IDispatch by looking up the “Open” method and Invoking that function if it exists. The equivalent Xojo code (without use of Operator_Lookup or ParamArray) would look something like this:
Dim Cnxn As New OLEObject("ADODB.Connection") ' Open connection Dim params(2) As Variant params(0) = "Pubs" params(1) = "MyUserId" params(2) = "MyPassword" Cnxn.Invoke("Open", params)
While functional, it doesn’t look or feel as nice as the VB equivalent. Enter Operator_Lookup. This operator overloads the class’s lookup operator. So whenever you call a method or property, for example Object.MyMethod, the Operator_Lookup is called with the name of the method or property (in this case “MyMethod”) passed as a parameter. By overloading this operator on the OLEObject class, you can now do something like this:
Underneath the hood the OLEObject implementation of Operator_Lookup looks something like this:
Sub Operator_Lookup(name As String, params() As Variant) Me.Invoke(name, params) End Sub
Now that we’ve overloaded Operator_Lookup the next step to making this code look more like VB is to use ParamArray. By using a ParamArray, the user can now pass an indefinite number of parameters to the function without formally using an array. The final Operator_Lookup function would look something like this:
Sub Operator_Lookup(name As String, ParamArray params As Variant) Me.Invoke(name, params) End Sub
Now our code can simply call Open with all the parameters supplied on the same line:
Dim Cnxn As New OLEObject("ADODB.Connection") Cnxn4.Open("Pubs", "MyUserId", "MyPassword")
In conclusion, while ParamArray is more useful in the general sense, Operator_Lookup can be useful in situations like this where simplification makes sense. To round out our COM support we also had to add an OLEParameter class because it’s quite common in the COM world to pass parameters by name/position. So even though our compiler doesn’t support passing parameters by name, by using the OLEParameter class you can specify which parameters and their exact types to pass.
Feel free to post any questions here.