How to extend any class

Post your Gambas programming questions here.
Post Reply
User avatar
tincho
Posts: 57
Joined: Wednesday 10th July 2019 1:12pm

How to extend any class

Post by tincho »

Hi,
After trying several methods to extend some classes like 'String' or 'File' I found a simple way to do it.
I'm going to do the example adding the function 'Words' to the class 'String'.
The first step is to create a class called 'String' in the current project, that is String.class, then inside this file:
' Gambas class file
Export
Static Public Function Words(s As String) As Integer

  Dim int As Integer
  Dim aString As New String[]

  s = String.Trim(s)

  If InStr(s, " ") > 0 Then
    aString = Split(s, " ")
    int = aString.Count
  Else
    If Len(s) > 0 Then
      int = 1
    Endif
  Endif
  Return int
End
Regards
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How to extend any class

Post by BruceSteers »

I do not think it is a good idea to name classes with the same name as already existing internal classes.
Might as well name the class StringF.class or something, not much difference between typing String.Words or StringF.Words and will avoid conflicts.

PS. here's your function shortened to 2 lines. ;) :roll:
Public Function Words(s As String) As Integer
  If Not s.Len Then Return 0
  Return Split(s, " ").Count
End
If at first you don't succeed , try doing something differently.
BruceS
User avatar
tincho
Posts: 57
Joined: Wednesday 10th July 2019 1:12pm

Re: How to extend any class

Post by tincho »

BruceSteers wrote: Thursday 20th May 2021 6:10am I do not think it is a good idea to name classes with the same name as already existing internal classes.
I was thinking the same thing, but so far there are no conflicts, I guess because I don't overwrite existing methods.
It's an experiment to tune some functions that I would like to add to the real String, maybe someday.
PS. here's your function shortened to 2 lines. ;) :roll:
Great, very good code and more efficient. Thanks
Image
Regards
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How to extend any class

Post by BruceSteers »

tincho wrote: Thursday 20th May 2021 7:58am
BruceSteers wrote: Thursday 20th May 2021 6:10am I do not think it is a good idea to name classes with the same name as already existing internal classes.
I was thinking the same thing, but so far there are no conflicts, I guess because I don't overwrite existing methods.
It's an experiment to tune some functions that I would like to add to the real String, maybe someday.
PS. here's your function shortened to 2 lines. ;) :roll:
Great, very good code and more efficient. Thanks
Image
Regards
oops i forgot to Trim() may not be as fast now.
Public Function Words(st As String) As Integer
Dim s As String = Trim(st)
  If Not s.Len Then Return 0
  Return Split(s, " ").Count
End
I guess if you have no problems then go for it
If at first you don't succeed , try doing something differently.
BruceS
User avatar
tincho
Posts: 57
Joined: Wednesday 10th July 2019 1:12pm

Re: How to extend any class

Post by tincho »

oops i forgot to Trim() may not be as fast now.
Still faster that the original function: 13.617 vs 7.294
I guess if you have no problems then go for it
Hehe, that's true, although problems usually come without you calling them. Problem solving is what brought us to where we are, (I mean humanity), but it can be interpolated for programming as well. :D
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How to extend any class

Post by BruceSteers »

tincho wrote: Thursday 20th May 2021 11:14am
oops i forgot to Trim() may not be as fast now.
Still faster that the original function: 13.617 vs 7.294
I guess if you have no problems then go for it
Hehe, that's true, although problems usually come without you calling them. Problem solving is what brought us to where we are, (I mean humanity), but it can be interpolated for programming as well. :D
Yep lol , I think humanity needs much more bugfix than antivirus to be honest.

If you compile gambas from source (using git to pull) you can create your own branch and then add your code to String.class in gb.util

Hmm actually it looks like gb.util just does the same as you do by making a String.class so i guess your way will be sound unless you use gb.util
If at first you don't succeed , try doing something differently.
BruceS
User avatar
tincho
Posts: 57
Joined: Wednesday 10th July 2019 1:12pm

Re: How to extend any class

Post by tincho »

BruceSteers wrote: Thursday 20th May 2021 12:30pm Hmm actually it looks like gb.util just does the same as you do by making a String.class so i guess your way will be sound unless you use gb.util
Its correct, indeed my two targets are File.class & String.Class both inside gb.util
But i test in a project (tradukisto) at the same time gb.util and my classes File.class & File.class everything works perfectly.
Post Reply