[Gambas-user] How to extend the String[] class ??

Laurent Carlier lordheavym at gmail.com
Thu Nov 23 18:20:54 CET 2023


Le jeudi 23 novembre 2023, 14:02:21 CET Benoît Minisini a écrit :
> 
> You can't create a class in the IDE with the "[]" characters in the
> name, so you can't extend the "String[]" class.
> 
> "[]" in a class name is a bit special. "String[]" is a normal class,
> but, for example, "Collection[]" or "String[][]" is an array of a normal
> class. It's a normal class, except that it is automatically created at
> runtime the first time you use it.
> 
> So allowing "[]" in a class name may lead to some problems at the
> moment. I must think about that.
> 
> But you can use normal class name for inheriting "String[]", it should work.
> 
> Regards,

I can understand that it's complicated to implement such behavior. I created a 
module to add these "workaround", eg i wanted to append two arrays, or to add 
a clamp:

---->8----

 ' Clamp Value between Minimal and Maximal
Public Sub Clamp(Value As Integer, Minimal As Integer, Maximal As Integer) As 
Integer

  If Value < Minimal Then Value = Minimal
  If Value > Maximal Then Value = Maximal

  Return Value

End

' Append Source array to Destination array - We act on object so no need to 
return a new one !
Public Sub Append(Source As Object, Destination As Object)

  Dim Value As Variant

  If Comp(Object.Type(Source), Object.Type(Destination)) <> 0 Then 
Error.Raise("Source and Destination must be of the same type!")
  If InStr(Object.Type(Source), "[]") = 0 Then Error.Raise("Source and 
Destination are not arrays!")

  For Each Value In Source
    Object.Call(Destination, "Push", [Value])
  Next

End

----8<----

Regards

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20231123/2f3b3bb8/attachment.sig>


More information about the User mailing list