Programming with Xojo from scratch: class modules and extensions

Programming with Xojo from scratch: class modules and extensions

We continue with the Programming Course with Xojo… and since, as you already know, it is a cross-platform development environment, nothing better than continuing the development of our sample project (Personal Expenses) this time… under Linux!

That's right, projects created with Xojo can be opened by the IDE from any supported platform: macOS, Windows and Linux.

We could also develop the application in the IDE from macOS and run it in debug mode on a remote computer on our local network with another operating system: Windows and Linux. Not bad at all, right?



But back to what concerns us, this time we will expand the application so that the general list shows us in an additional column the accumulated total for each of the categories.

As you will remember, when we implemented the hierarchical list in the previous chapter, we saw how the expenses were grouped by category, so that you can view each of them to see all the records (expense items), as well as the amount associated with each of them. they.

However, it would be nice to be able to see what the accumulated spend is for the category without having to explain and add up each of the expenses yourself. And that's what you can see in the video accompanying this new chapter.

The important issue here is that we will use two new elements provided by the Xojo development language to do this. One of them is Forms and the second is Class extensions.

https://youtu.be/me6sr9hdjm4

Moduli in Xojo

You can think of Modules as a tool provided by the Xojo programming language that allows us to create our own "library" of functions (methods), properties, classes, other modules and, ultimately, any element supported by the language.



This is important because creating these libraries or modules gives us enormous flexibility.

For example, on the one hand we can indicate that all elements added to the module have a Global scope, which means that we can use them (invoke them, in the case of methods; or access them, if they are properties), from anywhere else in the app.

It also implies that a well-created library will allow us to use it over and over again in any application we develop without having to rewrite the same code over and over again.

That is, once you have added the functionality provided in the library, it will be enough to simply embed the module in any other app you want to create… and use that functionality.

If you would like more information on what modules are and how to use them, I recommend that you take a look at this section of the online documentation. 

Class extensions in Xojo

The second problem is the class extensions. Well, let me explain. Sometimes it happens that we would find it interesting to add functionality to an existing class.

The problem is that we may not be the original developers of the class in question, which means that we do not have access to the source code so that we can modify or "extend" its functionality; just as happens, for example, with the classes that you find already available in the same Xojo Framework, without going further.

How can we do then in these cases? Well, the answer lies in the use of the keyword "Extends" at the beginning of the definition of a new method (for example in a Module).


In this way we indicate to the Xojo compiler that said method will "extend" or "extend" the capacity of the data type that we indicate below as the first parameter.



Once created, we will be able to use this method using dot notation as if it were a method originally added to the class in question.

This is something you will see much more clearly in the video accompanying this chapter of the course; Among other things, because this is the technique we use precisely to calculate the accumulated total of all the expense items of the same category: we add a new method in a module with "Extend" which receives as its first parameter an array of data of type Expense (Remember this is the class we created to add new expense items).

Then, the method code just needs to iterate through each of the array elements and add the different amounts, returning the total value as a result of the call.

As usual, you can learn more about the " Extends ”Keyword in realbasic online documentation.

Try creating your own sample project, adding a module and one or more methods that extend the capabilities of several existing classes by creating new methods in the module.


Javier Rodriguez Xojo Engineer Twitter: @xojoes Facebook: http://facebook.com/xojoes/ Download the latest version from www.xojo.com Xojo Resources in Spanish: https://docs.xojo.com/spanish

add a comment of Programming with Xojo from scratch: class modules and extensions
Comment sent successfully! We will review it in the next few hours.