Operator_Lookup Method

Used to specify a lookup value for an otherwise undefined property of a class.

Syntax


Notes

Use Operator_Lookup to define a function that will be called if the called item cannot otherwise be found in the class.

Operator_Lookup overloads a class's lookup operator. The method's first parameter must be a String which will receive the name of the desired member. The method can have any combination of other parameters and can have any return type. The Operator_Lookup operator is called whenever you use the foo.bar syntax to look up a member of an object and the compiler can't find anything named "bar". Before it returns an Undefined Identifier error, the compiler will try to call a lookup operator and pass "bar" as the first parameter. Overloading works for the lookup operator just as it does for any other method.

Please be advised that this can be a dangerous thing to do because you may, for example, inadvertently call the Operator_Lookup function with a misspelling and you don't get an error message.


Example

This example defines "SquareLength" in the Vector class (see Operator_Add), a class with two Integer properties, x and y.

Function Operator_Lookup (Name as String) as Double
 If Name = "SquareLength" then Return Self.x^2 + Self.y^2

When you write:

Dim myDouble as Double
myDouble=myVector.SquareLength

it will return the result of the calculation. However, you can also write:

myDouble=myVector.UndefinedFunction  //does not exist

and it will compile, returning zero. This can lead to difficult to track down errors.