Extends You use in (nearly) every project

Hi All,

I just updated an Extends that Bob suggested back in 2015 to set a DesktopPopupMenu to the correct row.
TBF, Bob’s version was API 1, I just updated it to API 2.

Sub SetId (Extends pm as DesktopPopupMenu, id as Integer)
if id < 1 then
  pm.SelectedRowIndex = -1
  return
end if

for i as Integer = 0 to pm.RowCount - 1
  if pm.RowTagAt(i) = id then
    pm.SelectedRowIndex = i
    return
  end if
next

What extends or methods do have / use in (nearly) every project?
I feel like I am missing alot…

Thanks.

Hi Craig,

here you may could use „SelectRowWithTag“ directly in newer Xojo versions. I guess it was introduced with API 2!?

best, Thomas

I have many…

I use the venerable StringUtils module, which has scores of useful extension methods (some of which may have been subsequently incorporated into Xojo) to which I’ve added a number of my own over the years.

I also have a few extensions to ListBox:

  • Column, which returns the index of the first column whose heading matches the passed string
  • AddColumn
  • AddRowWithTag (makes code a little cleaner)
  • SelectAllRows
  • DeleteSelectedRows
  • DeselectAllRows
  • ToCSV

For Window I have

  • Center
  • ContainerControls as ContainterControl()

For FolderItem I have DeleteFolder (deletes entire folder and contents).
For Integer I have some bit getters and setters.
For Boolean I have IntegerValue
For Graphics I have CenterText

The challenge is often remembering that I have all these utility functions and not reinventing them.

2 Likes

I have an external utility module, that is in all my projects, and that contains all the useful methods.
along the years, there may be hundreds of them…

Julia,

I find Extends confusing. Could you just provide the code for one of these examples, perhaps the Boolean so I might understand how this is done.

Thanks

Public Function ToInteger(extends b as Boolean) As Integer
  If b Then Return 1
  Return 0
  
End Function

2 Likes

Jean-Yves’ example is almost exactly what I have for Boolean to Integer.

In general, Extension methods just make code cleaner and more readable. You tend to end up with a series of dot notation calls instead of many nested calls in parentheses. For example, using a regular method, calling my ListBox column index from header function looks like this:

Column(myListBox, "TheHeading")

whereas with an extension method it’s

myListBox.Column("TheHeading")

Here’s the function:

Public Function Column(Extends lb As Listbox, Heading As String) As Integer
  
  For c As Integer = 0 To lb.LastColumnIndex
    If lb.HeaderAt(c) = Heading Then Return c
  Next
End Function
2 Likes
Module Module_TEST // Put it in module, scope Global

  // Extending the Boolean type
  Global Function ToInteger(extends boolvalue as Boolean) As Integer
  
    Return If(boolvalue, 1, 0)
  
  End Function

End Module

// Using it --------------------------------------

Var b As Boolean // default to false

Var i As Integer = b.ToInteger // 0, using the extension

System.DebugLog i.ToString  // 0

b = Not b // True

i = b.ToInteger // using the extension

System.DebugLog i.ToString  // 1

app.DoEvents // print debugs

Quit

1 Like

Hi Thomas,

Thanks for the reminder. :grinning:
I was a little distracted and tired when I posted this so it is not very surprising that I forgot / missed this entirely.

The gist of what I was getting at by asking the broader question is pretty much what has played out here. I have a better idea of the sorts of things people have and use.
This then helps me better understand where and what kinds of utilities I could write myself to help me move my stuff along.
Seeing a couple of lists of the sort of things people have posted have given me all sorts of thoughts!

I added these:

Sub DoesNotBeginWith (extends s As String, needle As String) as Boolean
// Complementary function to .BeginsWith
Return s.BeginsWith(needle)=False
End Sub

and:

Sub DoesNotEndWith (extends s As String, needle As String) as Boolean
// Complementary function to .EndsWith
Return s.EndsWith(needle)=False
End Sub

Then we have (with two signatures):

Sub DoesNotContain (extends i() As integer, myvar As Integer) as Boolean
Return  i.IndexOf(myvar)<0
End Sub

Sub DoesNotContain (extends s As String, needle as String) as Boolean
Return s.Contains(needle)=False
End Sub

Many possibilities.

In many of my projects, I use extensions for BinaryStream:
WriteLongPString and ReadLongPString: like their PString equivalent, but without length limit (length of string+string)
WritePicture and ReadPicture: write or retrieve picture, using the LongPString above with the picture converted to a string
WriteColor and ReadColor: write/read Color.Red, Color.Green and Color.Blue.
And so on…

2 Likes