Using Operator_Lookup

I’ve been using an example application (based on this blog post: https://blog.xojo.com/2014/01/27/saving-preferences/) to create preferences in JSON format. It’s working fine, but I’d like to understand the code in a little more detail (for reasons below [1]). The part that’s tripping me up is the use of Operator_Lookup. In the example it seems to be used to overload the accessor methods Get and Set.

        Function Operator_Lookup(name As String) As Variant
	  // Allow lookup of preference using this syntax:
	  // Dim top As Integer = Preferences.MainWindowTop
	  Return Get(name)
	End Function

	Sub Operator_Lookup(name As String, Assigns value As Variant)
	  // Set a preference using this syntax:
	  // Preferences.MainWindowTop = 345
	  Set(name) = value
	End Sub

However, I can’t see anything that actually uses these overloaded methods.

I’ve looked up Operator_Lookup in the documentation, where it says “If you implement Operator_Lookup on a class, you will no longer get compilation errors if you, for example, inadvertently misspell a property name. Instead, the Operator_Lookup method is called at runtime and is passed the name of your incorrectly spelled property.” I’m afraid I can’t understand this; it seems to be there to catch potential misspelling of property names.

Can someone enlighten me as to how and why this code is used? Can I actually just remove it to simplify the code?

BTW this is the “why” that I forgot to put in my post. The example code creates a JSON preferences file on just one level. I want to have a second level of hierarchy. My preferences file needs to look like this:

{
  "WinMainLeft":567,
  "WinMainTop":108,
  "WinPreferencesLeft":675,
  "WinPreferencesTop":254,
  "SelectedPPInstance":0,
  "ppInstances":[
    {
      "UserID":"myuserid1",
      "ServerFriendlyName":"Descriptive name 1",
      "ServerURL":"https:\\/\\/foo.com\\/foldername",
      "ServerDescription":"Description of server connection"
    },
    {
      "UserID":"admin",
      "ServerFriendlyName":"Descriptive name 2",
      "ServerURL":"https:\\/\\/bar.com\\/foldername",
      "ServerDescription":"This is another server connection"
    }
  ]
}

So I am going to need to elaborate my version to allow for more hierarchy. Hence the need to understand exactly how it works!

The actual work is done in the .Get and .Set methods which reads from/writes to the .mPreferences JSONItem

What Operator_Lookup does is makes it possible to have access to undefined property names. For example, if you define:

Function Operator_Convert(name as String) as String Return name + “_1” End Function

You could do something like this:

dim s as String = myClass.Xyz

“s” would be set to “Xyz_1”

If Foobar is not already defined in the class as a method or property, this method gets called with the undefined name as a string.

NOTE: Be.very careful with this method. Using it can prevent the compiler from showing you error messages for undefined properties.

This should be explained here:

http://developer.xojo.com/userguide/advanced-oop-features$operator-lookup