{"id":223,"date":"2019-08-06T12:08:43","date_gmt":"2019-08-06T18:08:43","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=223"},"modified":"2020-01-16T21:41:55","modified_gmt":"2020-01-17T04:41:55","slug":"interfaces","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/08\/06\/interfaces\/","title":{"rendered":"Interfaces"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, suppose you need a class that is a &#8220;List&#8221;. You could write a single class, called list, that you could add items to, remove items from, and generally manipulate in a &#8220;list like way&#8221;. 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 (<a href=\"https:\/\/en.wikipedia.org\/wiki\/List_(abstract_data_type)\">as wikipedia notes<\/a>)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Operations\nImplementation of the list data structure may provide some of the following&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Operation_(mathematics)\">operations<\/a>:\n- a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Constructor_(computer_science)\">constructor<\/a>&nbsp;for creating an empty list;\n- an operation for testing whether or not a list is empty;\n- an operation for prepending an entity to a list\n- an operation for appending an entity to a list\n- an operation for determining the first component (or the \"head\") of a list\n- 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.)\n- an operation for accessing the element at a given index.\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">But note that wikipedia, and most other place that have such a &#8220;spec&#8221; dont say how this is implemented. Just what the API is. And this is a perfect place to use an interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So I would NOT add this to the interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But everything else can be specified in an interface.\n<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>an operation for testing whether or not a list is empty<\/em> - possibly a method named \"IsEmpty\" that returns a boolean ?<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>an operation for prepending an entity to a list<\/em> - possibly a method named \"Prepend\" that takes an element and adds it to the \"front\" of the list<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>an operation for appending an entity to a list<\/em> - possibly a method named \"Append\" that takes an element and adds it to the \"end\" of the list<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>an operation for determining the first component (or the \"head\") of a list<\/em> - maybe a method called \"FirstItem\" or \"Head\" that returned the first item<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>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.)<\/em> a method called \"Tail\" that returns the list with the first item removed\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>an operation for accessing the element at a given inde<\/em>x - a method called \"ElementAt\" that takes an index parameter and returns the element at that index<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And that would be an interface that confirmed to Wikipedias notion of &#8220;List&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The interesting thing is that Xojo already has many classes that behave in ways that are &#8220;list like&#8221; in many ways. Listbox, popupmenu,combobx and a few others already have methods like AddRow, RemoveRow and many of the others that are &#8220;list manipulation and inquiry&#8221; type methods. You can find out if a listbox is empty (listcount = 0), you can remove and access rows at specific positions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But Xojo doesnt define and use an interface for this class or any other that share similarties. However, you can add your own.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Still you might come up with an API for &#8220;list&#8221; like things that looks like :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Interface List\n  Sub AddRow(ParamArray values() as string)\n  End Sub\n  \n  Sub AddRowAt(ParamArray values() as string, zeroBasedInxed as integer)\n  End Sub\n  \n  Sub FirstRowIndex() as integer\n  End Sub\n  \n  Sub LastAddedRowIndex() as integer\n  End Sub\n  \n  Sub LastRowIndex() as integer\n  End Sub\n  \n  Sub RemoveAllRows()\n  End Sub\n  \n  Sub RemoveRowAt(zeroBasedIndex as integer)\n  End Sub\n  \n  Sub RowCount() as integer\n  End Sub\n  \n  Sub RowTag() as Variant\n  End Sub\n  \n  Sub RowTagAt(zeroBasedIndex as integer) as variant\n  End Sub\n  \n  Sub RowValue() as Variant\n  End Sub\n  \n  Sub RowValueAt(zeroBasedIndex as integer) as Variant\n  End Sub\n  \n  Sub SelectedRowCount() as Integer\n  End Sub\n  \n  Sub SelectedRowIndex() as integer\n  End Sub\nEnd Interface\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>If &lt;something that implements list> IsA List Then\nEnd If\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is very handy and very powerful and wildly under utilised.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;List&#8221;. You could write a single class, called list, that you could add items to, remove items from, and generally &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/08\/06\/interfaces\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Interfaces&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[27,6,3],"class_list":["post-223","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-advanced-techniques","tag-tips","tag-xojo"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":3,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":599,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions\/599"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}