


Dim pr As New MyPreferencesĪs you can see, Page, Index and BaseColor are not members of our class these are captured as Keys for the Dictionary thanks to the Operator_Lookup operator. 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 Operator_Lookup method. 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. Once implemented, we can take advantage of the feature.
#XOJO CLASSES EXAMPLES CODE#
Writing the following line of code in the resulting Code Editor: data.Value( key ) = value Parameters: key as string, assigns value as auto.This is, the overload for the Operator_Lookup method: Now we will add the method that we are really interested in. This property will act as the storage for the Key/Value pairs used for our Preference Class.Īdd now a new Constructor method in charge of initializing the just defined Dictionary property:Īnd put this line of code in the Code Editor for the Constructor method: Data = New Then, we will add a property with Data as the Name and as the data type, setting its scope to Private (this means that the property will be accessible only from the own class). For this example, we will use MyPreferences as the name of the class. 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. This way we can use the simple dot notation both to store and assign the stored values. 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.

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. 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. But we also have at our disposal another operator we can overload: Lookup. These are the methods in charge of adding two instances of the same class, subtracting, multiplying or dividing them. For example, we can overload the operators. We have seen in other posts that some of these overloaded methods can be Class Constructors, but, there are others things you can do. Xojo is an Object Oriented Programming Language and, among other things, that means that it supports Methods Overloading.
