Interfaces are one of those things in Xojo, and many other computing languages, that can really help you make your code more reusable and generic.
For instance, suppose you need a class that is a “List”. You could write a single class, called list, that you could add items to, remove items from, and generally manipulate in a “list like way”. You might go so far as to look up some other languages implementation of list and create a Xojo equivalent. But in general you would have (as wikipedia notes)
Operations Implementation of the list data structure may provide some of the following operations: - a constructor for creating an empty list; - an operation for testing whether or not a list is empty; - an operation for prepending an entity to a list - an operation for appending an entity to a list - an operation for determining the first component (or the "head") of a list - an operation for referring to the list consisting of all the components of a list except for its first (this is called the "tail" of the list.) - an operation for accessing the element at a given index.
But note that wikipedia, and most other place that have such a “spec” dont say how this is implemented. Just what the API is. And this is a perfect place to use an interface.
Now in Xojo MOST times you dont need to define the CONSTRUCTOR in an interface. You can but it is unusual and depending on what classes you intend to have implement this interface there can be restrictions on which constructors must exist (ie/ if you want a UI control like listbox to implement this interface it may need a constructor with no parameters)
So I would NOT add this to the interface.
But everything else can be specified in an interface.
an operation for testing whether or not a list is empty - possibly a method named "IsEmpty" that returns a boolean ?
an operation for prepending an entity to a list - possibly a method named "Prepend" that takes an element and adds it to the "front" of the list
an operation for appending an entity to a list - possibly a method named "Append" that takes an element and adds it to the "end" of the list
an operation for determining the first component (or the "head") of a list - maybe a method called "FirstItem" or "Head" that returned the first item
an operation for referring to the list consisting of all the components of a list except for its first (this is called the "tail" of the list.) a method called "Tail" that returns the list with the first item removed
an operation for accessing the element at a given index - a method called "ElementAt" that takes an index parameter and returns the element at that index
And that would be an interface that confirmed to Wikipedias notion of “List”
The interesting thing is that Xojo already has many classes that behave in ways that are “list like” in many ways. Listbox, popupmenu,combobx and a few others already have methods like AddRow, RemoveRow and many of the others that are “list manipulation and inquiry” type methods. You can find out if a listbox is empty (listcount = 0), you can remove and access rows at specific positions.
But Xojo doesnt define and use an interface for this class or any other that share similarties. However, you can add your own.
In order to do this you need to define the interface in a very generic way so that adding a row to a listbox, which may have 1 or more columns, still makes sense. Some input parameters might need to be variants instead of something more specific. And, for some things the right return value may have to be a variant instead of something more specific.
Still you might come up with an API for “list” like things that looks like :
Interface List Sub AddRow(ParamArray values() as string) End Sub Sub AddRowAt(ParamArray values() as string, zeroBasedInxed as integer) End Sub Sub FirstRowIndex() as integer End Sub Sub LastAddedRowIndex() as integer End Sub Sub LastRowIndex() as integer End Sub Sub RemoveAllRows() End Sub Sub RemoveRowAt(zeroBasedIndex as integer) End Sub Sub RowCount() as integer End Sub Sub RowTag() as Variant End Sub Sub RowTagAt(zeroBasedIndex as integer) as variant End Sub Sub RowValue() as Variant End Sub Sub RowValueAt(zeroBasedIndex as integer) as Variant End Sub Sub SelectedRowCount() as Integer End Sub Sub SelectedRowIndex() as integer End Sub End Interface
And then you can apply this to your own classes. custom subclasses of listbox, combobox, popup menu and other controls that have list like aspects to them.
Once you do this you can then write generic methods that manipulate Lists, without regard to whether its a custom user class implementing the interface, a listbox, a popupmenu etc because all of them will return TRUE when you do
If <something that implements list> IsA List Then End If
This is very handy and very powerful and wildly under utilised.