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

Gianluigi gradobag at gradobag.it
Thu Nov 23 18:23:18 CET 2023


Il 23/11/23 17:54, T Lee Davidson ha scritto:
> On 11/23/23 08:02, Benoît Minisini wrote:
>> 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,
>>
>> -- 
>> Benoît Minisini.
>
> So then, something like this:
>
> [StringArray.class]
> ' Gambas class file
>
> Export
> Inherits String[]
>
> Public Sub Squash() As String[]
>
>   Return Split(Super.Join(","), ",", Null, True) ' I like that code, 
> Guianluigi; very slick.
>
> End
> [/StringArray.class]
>
>
> [Main.module]
> ' Gambas module file
>
> Public Sub Main()
>
>   Dim myStringArray As StringArray
>
>   myStringArray = ["risotto", "", "Pascualina", "focaccia"]
>   myStringArray = myStringArray.Squash()
>   For Each s As String In myStringArray
>     Print s
>   Next
>
> End
> [/Main.module]
>
>
Hi Lee,

very cool code 😎, then _put and _get are useless.

I'm afraid I stole the 'slick' code from vuott. 😇

Regards
Gianluigi




More information about the User mailing list