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!