Xojo is an Object Oriented Programming Language and, among other things, that means that it supports Methods Overloading. We have seen in other posts that some of these overloaded methods can be Class Constructors, but, there are others things you can do. For example, we can overload the operators. These are the methods in charge of adding two instances of the same class, subtracting, multiplying or dividing them. But we also have at our disposal another operator we can overload: Lookup. What advantages does this give us and how it does it work? Let’s explore it while building a Preferences class we can use in any of our projects.
When we define and implement the Operator_Lookup method in our classes, we will be able to use the dot notation to access and assign a value to a class member that has not been defined for the class! That is, it will be the code implemented in this method that will decide how it will act, not the Xojo Compiler. Among other possibilities, this will bring us the flexibility to create a Preferences class that is not attached to a particular project, and that is multiplatform, of course, backed by as many pairs of Keys/Values in a Dictionary as we need to store our apps preferences. This way we can use the simple dot notation both to store and assign the stored values. Let’s put this into practice.
The first step is adding a new Class item to the project without assigning a Super class; that is, this will be a base class. For this example, we will use
MyPreferences as the name of the class. Then, we will add a property with
Data as the Name and
Xojo.Core.Dictionary as the data type, setting its scope to Private (this means that the property will be accessible only from the own class). This property will act as the storage for the Key/Value pairs used for our Preference Class.
Add now a new Constructor method in charge of initializing the just defined Dictionary property:
- Name: Constructor
- Scope: Public
And put this line of code in the Code Editor for the Constructor method:
Data = New Xojo.Core.Dictionary
Now we will add the method that we are really interested in. This is, the overload for the Operator_Lookup method:
- Name: Operator_Lookup
- Parameters: key as string, assigns value as auto
- Scope: Public
Writing the following line of code in the resulting Code Editor:
data.Value( key ) = value
Once implemented, we can take advantage of the feature. If we add the Open Event to the project and write the below snippet of code, we will see how we can use the dot notation to use and access class members that have never been added to the class. These members are captured by the
Operator_Lookup method, adding the member as the key for a new entry in the dictionary (this is the text we write after the dot) and assigning to it the value we put after the equal symbol (the assignation operator); thanks to the use of the
assigns keyword in the parameters declaration for the
Dim pr As New MyPreferences pr.page = 10 pr.index = "20" pr.baseColor = &c204060
As you can see,
BaseColor are not members of our class; these are captured as Keys for the Dictionary thanks to the Operator_Lookup operator. In addition, you can see how we can assign several types of values to these: an Integer, a String and a Color value.
Retrieving data with Operator_Lookup
Now, how can we retrieve values for these not existing members using the dot notation? As you probably guessed, we need to add an additional method that overloads Operator_Lookup. In this case the method signature will accept as the parameter the Key we want to retrieve (the class member put after the dot), returning always a String, in order to simplify this example. Thus, add a new method for the class using the following signature:
- Name: Operator_Lookup
- Parameters: Key As String
- Return Type: String
- Scope: Public
Writing the following code in the associated Code Editor:
Dim a As Auto Dim s As String If data.HasKey(key) Then a = data.Value(key) Dim info As Xojo.Introspection.TypeInfo info = xojo.Introspection.GetType(a) If info = GetTypeInfo(Text) Or info = GetTypeInfo(String) Then s = a Elseif info = GetTypeInfo(Integer) Then s = CType(a, Integer).totext Elseif info= GetTypeInfo(Color) Then Dim c As Color = a s = Str(c) End If End If Return s
As you can see, and after verifying that the Dictionary has the Key, we use the Introspection mechanisms provided by the language in order to see what type is associated with the key. Once we have its type, we will convert the value to a String using the appropriate approach for each case: an Integer, a Text/String or a Color. Of course, for brevity, this example doesn’t take into account other possible value types; but that is something you can implement yourself!
Let’s go back to the Open Event for the project and add the following line of code at the end:
MsgBox pr.page + EndOfLine + pr.index + EndOfLine + pr.baseColor
Run it and observe how we can assign values and retrieve them (always as a String) simply using the dot notation. We can take this class from one project to other and use the dot notation to reference the elements we want to save and retrieve as preferences.
If you decide to use this approach for your projects, there are some considerations you have to take into account. For example, you will not be able to take advantage of the Autocompletion feature provided by the IDE. In addition, we will have to pay special attention to write exactly the same “member” in all the cases we want refer to it; otherwise we will be using a different key with unexpected results. After all, once implemented, Operator_Lookup for the class the Compiler will not complain about the undefined members you are trying to access!
It would also be very easy to expand the class so it can work with other data types; adding an additional method to serialize the Key/Values pairs so they can be stored on file/database for a later retrieval and reconstruction via an additional Constructor. Finally, it is very probable that you only want to use a MyPreferences instance in your app, so you could implement this class as a Singleton. Do you dare? If you do, tell me about it!
Javier Rodriguez has been the Xojo Spanish Evangelist since 2008, he’s also a Developer, Consultant and Trainer who has be using Xojo since 1998. He manages AprendeXojo.com and is the developer behind the GuancheMOS plug-in for Xojo Developers, Markdown Parser for Xojo, HTMLColorizer for Xojo and the Snippery app, among others