Extend for an Array

I need to support both API1.0 and 2.0 :frowning: (like most of the rest of us)
So I started to create a module that had a number of EXTENDS
for example

Public Sub API2_DrawingColor1(extends g as Graphics, assigns clr as color)
  // Xojo2019r2 : API2.0 compatiblity code
  #If XojoVersion >= 2019.02 Then
    g.DrawingColor=clr
  #Else
    g.ForeColor=clr
  #EndIf
End Sub

But I am trying to figure out how to extend things that are not attributes of an existing class

Like UBOUND

Private Function API2_LastRowIndex( ????? ) as integer
  // Xojo2019r2 : API2.0 compatiblity code
  #If XojoVersion >= 2019.02 Then
    Return ??????.LastRowIndex
  #Else
    Return ?????.Ubound
  #EndIf
End Function

you’re extending the CLASS not one of its attributes

so maybe I’m misunderstanding the question ?

Seems nobody understands the question I asked.
I don’t want to extend myClass… I have an ARRAY (don’t care what datatype it is)
All I want it the UBOUND/LastRowIndex… in a manner that is API1 / API2 “safe”

you’d need to extend whatever the data of that array is
and you can only do a single dimnesion array

Private Function API2_LastRowIndex( extends stringArray() as string ) as integer
  // Xojo2019r2 : API2.0 compatiblity code
  #If XojoVersion >= 2019.02 Then
    Return stringArray.LastRowIndex
  #Else
    Return stringArray.Ubound
  #EndIf
End Function

So, sigh, the answer is IT CANNOT BE DONE… in a “generic” manner

I am trying to create a library of functions to get around all these stupid API name changes so I don’t have to maintain two codebases

Generics as in only use ‘API2_LastRowIndex’?
You can define the same function name with each datatype, no?

sure, you can overload a function… but the point is… What ARE “each datatype”?

And I don’t mean, Integer, String, Double etc…

I mean myClass, YourClass, theOtherClass and who knows what someone (myself included) might come up with for the next project

Yeah, I suspected you didn’t want something like that. I hope you find a way to do it.

[quote=460560:@Dave S]I mean myClass, YourClass, theOtherClass and who knows what someone (myself included) might come up with for the next project

[/quote]

[code]Public Function API2_LastIndex(extends mArray() as Object) as integer
// Xojo2019r2 : API2.0 compatiblity code
#If XojoVersion >= 2019.02 Then
Return mArray.LastRowIndex
#Else
Return mArray.Ubound
#EndIf

End Function
[/code]

Should cover Arrays of any type of Object. You’d still have to slog through the intrinsic types by hand though…

Private Function UboundGeneric(extends objectArray() as Object) as Integer #if XojoVersion >= 2019.02 then Return objectArray.LastRowIndex #else Return objectArray.Ubound #endif End Function

Here’s my test harness code

[code]dim a() as Class1
for intCycle as Integer = 1 to 100
a.Append( new Class1 )
next

dim x as Integer = UboundGeneric( a )
break[/code]

That CAN work as an EXTENDS!

Public Function API2_LastRowIndex(extends objectArray() as Object) as integer
  #If XojoVersion >= 2019.02 Then
    Return objectArray.LastRowIndex
  #Else
    Return objectArray.Ubound
  #endif
End Function
dim x as integer=zItems.API2_lastRowIndex

I also coded a method for storage types. It’s quite a bit of code, but it gets the job done the best it can be in Xojo as-is, I believe. If someone comes up with something better, I’d love to take a look.

See “Usage” note in API2Compat module for more info. This module is provided free for personal or commercial use.

Project File

Update
Made the usage for storage types a bit better with CType rather than variable casting.

Here are the routines I have for handling object Arrays across the API versions
(Thanks Anthony for the right direction to head)

Public Sub API2_AddRow(extends objectArray() as object, item as object)
  // Xojo2019r2 : API2.0 compatiblity code
  #If XojoVersion >= 2019.02 Then
    objectArray.AddRow item
  #Else
    objectArray.Append item
  #EndIf
End Sub

Public Sub API2_AddRowAt(extends objectArray() as object, index as integer, item as object)
  // Xojo2019r2 : API2.0 compatiblity code
  #If XojoVersion >= 2019.02 Then
    objectArray.AddRowAt index,item
  #Else
    objectArray.Insert index,item
  #EndIf
End Sub

Public Function API2_LastRowIndex(extends objectArray() as Object) as integer
  #If XojoVersion >= 2019.02 Then
    Return objectArray.LastRowIndex
  #Else
    Return objectArray.Ubound
  #endif
End Function

Public Sub API2_RemoveRowAt(extends objectArray() as object, index as integer)
  // Xojo2019r2 : API2.0 compatiblity code
  #If XojoVersion >= 2019.02 Then
    objectArray.RemoveRowAt(index)
  #Else
    objectArray.Remove(index)
  #EndIf
End Sub

This allowed me to write code using 2019r1.1, that is compatible (no deprecation warnings) in 2019r2
and should I decided to go full blown R2 then I just dump the module that contains the above code, and remove all the “API2_” tags, a 2 minute operation at best

I did not post them here, but I did a similar thing for most of the graphics routines as well

Just one example :

Public Sub API2_DrawText(extends g As graphics, str As String, x As Double, y As Double, width As Double = 0, condense As Boolean = False)
  // Xojo2019r2 : API2.0 compatiblity code
  #If XojoVersion >= 2019.02 Then
    g.DrawText(Str, x , y, width, condense)
  #Else
    g.DrawString(Str, x , y, width, condense)
  #EndIf
End Sub